flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.67k stars 27.35k forks source link

[tool] Errors are not shown when running with `flutter run --machine` #92903

Closed arkanmgerges closed 4 weeks ago

arkanmgerges commented 2 years ago

Steps to Reproduce

I'm working on an app, and it breaks on a page in release mode, but it works in debug mode (there is no error in logs when it is in debug mode)

Note: The code is identical for both release and debug modes

Expected results: If there is an error it should show it in logs in debug mode

Actual results: Error in release mode

The page and screen files were the app crash in release mode: **home_page.dart** ```dart import '../screen/home_screen.dart'; import 'package:flutter/material.dart'; class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: HomeScreen(), ); } } ``` **home_screen.dart** ```dart import 'package:cafm/generated/l10n.dart'; import 'package:cafm/logic/cubit/auth_cubit.dart'; import 'package:cafm/presentation/widget/custom_elevated_button.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:recase/recase.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({Key? key}) : super(key: key); @override _HomeScreenState createState() => _HomeScreenState(); } class _HomeScreenState extends State { @override Widget build(BuildContext context) { final authState = context.read().state as Authorized; return SafeArea( child: Column( children: [ SizedBox( height: 20, ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Text('${S.of(context).hello.sentenceCase} ${authState.person!.firstName}', style: TextStyle(color: Theme.of(context).textTheme.bodyText2!.color, fontSize: 17)), CustomElevatedButton(callback: () { context.read().disallowAuth(); Navigator.of(context).pushReplacementNamed('/'); }, text: S.of(context).logout.sentenceCase), ], ), Divider(), Padding( padding: const EdgeInsets.only(left: 30.0, right: 30.0), child: Column( children: [ Align( alignment: Alignment.topLeft, child: Text('${S.of(context).myWork.sentenceCase}', style: TextStyle(color: Theme.of(context).textTheme.bodyText2!.color, fontSize: 20)), ), SizedBox(height: 10), Align( alignment: Alignment.topLeft, child: Expanded(child:CustomElevatedButton(callback: () {}, text: S.of(context).dailyCheck.sentenceCase, fillSpace: true)), ), Align( alignment: Alignment.topLeft, child: CustomElevatedButton(callback: () {}, text: S.of(context).maintenanceProcedures.sentenceCase, fillSpace: true), ), Align( alignment: Alignment.topLeft, child: CustomElevatedButton(callback: () {}, text: S.of(context).tickets.sentenceCase, fillSpace: true), ), Align( alignment: Alignment.topLeft, child: CustomElevatedButton(callback: () {}, text: S.of(context).workOrders.sentenceCase, fillSpace: true), ), ], ), ) ], )); } } ``` **Screenshot in debug mode** ![Screenshot_20211102-121401](https://user-images.githubusercontent.com/7836805/139828078-c63c6bf4-0391-442d-a26c-b3866d4a5368.jpg) **Screenshot in release mode** ![Screenshot_20211102-114306](https://user-images.githubusercontent.com/7836805/139823389-b1e8a53c-aaef-481e-8c40-c26d688e2278.jpg)
Logs [Gist - logs in debug mode](https://gist.github.com/arkanmgerges/2c0bbeb4a202c2c53849cd6bd1a29951) [Gist - logs in release mode](https://gist.github.com/arkanmgerges/9157eb703a04530953db32bb7b79dce5) **Flutter Analyze** ``` Analyzing cafm... info - Unused import: 'package:flutter/foundation.dart' - lib\data\model\project.dart:2:8 - unused_import info - The value of the local variable 'st' isn't used - lib\main.dart:45:9 - unused_local_variable info - Unused import: 'package:cafm/data/provider/exception/unauthorized_exception.dart' - lib\presentation\screen\app_settings_screen.dart:1:8 - unused_import info - Unused import: 'package:cafm/logic/cubit/auth_cubit.dart' - lib\presentation\screen\app_settings_screen.dart:4:8 - unused_import info - Unused import: 'package:flutter_bloc/flutter_bloc.dart' - lib\presentation\screen\app_settings_screen.dart:6:8 - unused_import info - Unused import: 'package:cafm/di/app_di.dart' - lib\presentation\screen\technician_login_screen.dart:3:8 - unused_import info - Unused import: 'package:cafm/data/model/technician.dart' - test\data\model\person_test.dart:4:8 - unused_import info - The value of the local variable 'appState' isn't used - test\data\model\person_test.dart:10:14 - unused_local_variable info - Unused import: 'package:bloc_test/bloc_test.dart' - test\logic\cubit\app_state_test.dart:3:8 - unused_import info - Unused import: 'package:bloc_test/bloc_test.dart' - test\logic\cubit\counter_cubit_test.dart:1:8 - unused_import info - Unused import: 'package:test/test.dart' - test\logic\cubit\counter_cubit_test.dart:3:8 - unused_import 11 issues found. (ran in 14.0s) ``` **Flutter Doctor** ```sh C:\Users\arkan\StudioProjects\cafm>flutter doctor -v [√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1320], locale en-US) • Flutter version 2.5.3 at C:\Users\arkan\dev\flutter\sdk • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (3 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at C:\Users\arkan\AppData\Local\Android\sdk • Platform android-30, build-tools 30.0.3 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [√] Android Studio (version 2020.3) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [√] IntelliJ IDEA Ultimate Edition (version 2021.2) • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2021.2 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart [√] VS Code (version 1.61.2) • VS Code at C:\Users\arkan\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • SM N975F (mobile) • RF8MA1ALYDL • android-arm64 • Android 11 (API 30) • Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.54 • Edge (web) • edge • web-javascript • Microsoft Edge 95.0.1020.40 • No issues found! ```
danagbemava-nc commented 2 years ago

Hi @arkanmgerges, your sample contains some third_party packages and custom widget definitions, please provide a sample without any third party packages and if possible, provide the custom widget definition.

Although, from this piece of code, I can see that you're using the Expanded widget incorrectly, that may be the cause of the issue, although, I do find it strange that it wasn't reported in debug mode.

Align(
                alignment: Alignment.topLeft,
                child: Expanded(child:CustomElevatedButton(callback: () {}, text: S.of(context).dailyCheck.sentenceCase, fillSpace: true)),
              ),
arkanmgerges commented 2 years ago

Hi @danagbemava-nc, and thanks for the reply. Let's suppose that my code is totally wrong, and this is not the point, I don't only want to fix my issue, but the problem is why in the debug mode does not show errors? This can be a nightmare for the developer (my personal opinion), as I develop a large app in the debug mode, and no errors shown (usually I'm using it without -v option), and when we want to use the release mode, errors can appear in the console. This is really bad (as for now in my knowledge until maybe someone can explain this behavior and how to deal with such situation)

danagbemava-nc commented 2 years ago

@arkanmgerges, I agree that it is strange which is why I made this statement.

although, I do find it strange that it wasn't reported in debug mode.

Which is why I need your full sample so that it can be properly investigated.

Kindly provide the the CustomElevatedButton so I can see if it can be sufficiently reproduced and the appropriate actions taken.

Thank you

arkanmgerges commented 2 years ago

@danagbemava-nc Thank you for your reply. I will prepare a new project and I will try to make it have the same issue and provide it for the investigation.

arkanmgerges commented 2 years ago

Hi @danagbemava-nc , I've created a new app with the issue successfully, and compressed it into zip file, you can download it from my google drive

I've tested it on my physical phone (Samsung Galaxy Note 10+), using the usb cable. and I tried the debug and release mode. In debug mode there is no error and the page will appear, while in release mode the gray screen will appear and the error messages will appear,

Thank you

danagbemava-nc commented 2 years ago

This is rather strange, when I run it in debug mode, I immediately got the error in the console. See attached logs.

verbose logs log.txt

normal logs normal.txt

arkanmgerges commented 2 years ago

I'm using Android studio

image

And all the logs are in the "Run" window bellow as shown:

image

arkanmgerges commented 2 years ago

Also my device specification is:

image

arkanmgerges commented 2 years ago

I ran the app (bug_app) in VSCode and I can see the error in the DEBUG CONSOLE

image

arkanmgerges commented 2 years ago

I don't know why I can not see this in Android Studio

danagbemava-nc commented 2 years ago

I just tried with Android Studio and I got the error.

I do find it very strange that you're not getting the error.

screenshot Screenshot 2021-11-03 at 07 03 26
logs ```console Launching lib/main.dart on M2007J20CG in debug mode... Running Gradle task 'assembleDebug'... Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 ✓ Built build/app/outputs/flutter-apk/app-debug.apk. W/FlutterActivityAndFragmentDelegate(25541): A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps. Debug service listening on ws://127.0.0.1:56314/-TKz8ghGw6Q=/ws Syncing files to device M2007J20CG... ======== Exception caught by widgets library ======================================================= The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget. The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData. Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a Align widget. The ownership chain for the RenderObject that received the incompatible parent data was: SizedBox ← CustomElevatedButton ← Expanded ← Align ← Column ← Padding ← Column ← MediaQuery ← Padding ← SafeArea ← ⋯ When the exception was thrown, this was the stack: #0 RenderObjectElement._updateParentData. (package:flutter/src/widgets/framework.dart:5835:11) #1 RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:5851:6) #2 RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:5873:7) #3 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5544:5) #4 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6194:11) ... Normal element mounting (16 frames) #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) #21 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) #22 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) ... Normal element mounting (4 frames) #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) #27 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) #28 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) ... Normal element mounting (42 frames) #70 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) #71 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) #72 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) ... Normal element mounting (259 frames) #331 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) #332 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36) #333 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6344:32) ... Normal element mounting (164 frames) #497 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14) #498 Element.updateChild (package:flutter/src/widgets/framework.dart:3422:20) #499 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16) #500 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11) #501 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5) #502 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:33) #503 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21) #504 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5) #505 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1143:15) #506 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1080:9) #507 SchedulerBinding.scheduleWarmUpFrame. (package:flutter/src/scheduler/binding.dart:863:7) (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch) ==================================================================================================== ```
arkanmgerges commented 2 years ago

Thank you @danagbemava-nc for letting me know, I will reinstall Android Studio, and let you know

arkanmgerges commented 2 years ago

Strange, the same thing, no error shown after reinstalling Android Studio, also I'm using my physical phone (Samsung Galaxy Note 10+)

image

danagbemava-nc commented 2 years ago

@arkanmgerges can you test with any other device to see if you still experience this issue? I don't believe that it could be an issue with your device, but this is a rather strange issue.

arkanmgerges commented 2 years ago

@danagbemava-nc It is the same, no errors

image

danagbemava-nc commented 2 years ago

With that same emulator, does it produce the error in vscode?

arkanmgerges commented 2 years ago

In VSCode I can see the error

image

arkanmgerges commented 2 years ago

@danagbemava-nc finally I solved the issue (by reading this page on overstack), I've reset Android Studio to the default settings, then I installed Dart and Flutter plugins, then It worked

Manage IDE Settings ---> Restore Default Settings...

image

image

Thank you @danagbemava-nc for your help

stevemessick commented 2 years ago

@danagbemava-nc If you include the --machine option on the command line the error is not generated. flutter run --machine

danagbemava-nc commented 2 years ago

@stevemessick, thanks for pointing that out, I can reproduce that.

logs `flutter run` [verbose.txt](https://github.com/flutter/flutter/files/7472821/log.txt) [normal.txt](https://github.com/flutter/flutter/files/7472824/normal.txt) `flutter run --machine` [machine_verbose.txt](https://github.com/flutter/flutter/files/7472822/machine_verbose.txt) [machine.txt](https://github.com/flutter/flutter/files/7472823/machine.txt)
code sample [bug_app.zip](https://github.com/flutter/flutter/files/7472857/bug_app.zip)
flutter doctor -v ```console [✓] Flutter (Channel master, 2.6.0-12.0.pre.591, on macOS 11.6 20G165 darwin-arm, locale en-GB) • Flutter version 2.6.0-12.0.pre.591 at /Users/nexus/dev/sdks/flutters • Upstream repository https://github.com/flutter/flutter.git • Framework revision 83991244da (2 hours ago), 2021-11-04 00:58:03 -0400 • Engine revision bbc4615ecd • Dart version 2.15.0 (build 2.15.0-268.0.dev) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.0) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [✓] VS Code (version 1.61.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.28.0 [✓] Connected device (3 available) • Nexus’ IPhone (mobile) • 00008020-001875E83A38002E • ios • iOS 15.0.2 19A404 • macOS (desktop) • macos • darwin-arm64 • macOS 11.6 20G165 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.69 • No issues found! ``` ```console [✓] Flutter (Channel stable, 2.5.3, on macOS 11.6 20G165 darwin-arm, locale en-GB) • Flutter version 2.5.3 at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (3 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.0, Build version 13A233 • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [✓] VS Code (version 1.61.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.28.0 [✓] Connected device (3 available) • Nexus’ IPhone (mobile) • 00008020-001875E83A38002E • ios • iOS 15.0.2 19A404 • macOS (desktop) • macos • darwin-arm64 • macOS 11.6 20G165 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.69 • No issues found! ```
Waqar144 commented 4 weeks ago

@DanTup

Sorry for the ping, don't know who the right person is. Can flutter daemon add a new event that reports these errors? Or is there a better solution to get these errors?

I am writing a plugin for a text editor and without error reporting it would be quite useless.

For context:

DanTup commented 4 weeks ago

@Waqar144 I'm not sure I understand the request. flutter daemon is used for managing devices and (IMO) it doesn't make sense to send any build/runtime errors there in my opinion.

If you're writing an editor plugin to run Flutted debug sessions I would expect you to run the app using flutter run --machine and these errors to show up in the output there (this is where VS Code is getting them - via the debug adapter).

However, if your editor supports the Debug Adapter Protocol, you may be able to avoid writing code to handle this yourself.. You could start a Flutter debug adapter using flutter debug-adapter and then you may get full debug support for free (the VS Code extension is using this debug adapter).

Waqar144 commented 4 weeks ago

@Dantup thanks for replying.

I am using flutter run --machine indeed, I just referred to it as flutter daemon because the docs about the protocol live under that name. The exception errors however don't show up and that is the main issue.

Thanks about the DAP hint, I will look into that. Our DAP implementation is too generic atm, so we will need to handle custom requests.

DanTup commented 4 weeks ago

@Waqar144 if you can reproduce an issue where flutter run --machine is not providing you with an error you expect, please open a new issue with a sample project - that sounds like a bug. Off the top of my head, I'm wondering if it might be related to structured errors being on by default (which means they're only going over the VM Service).

Our DAP implementation is too generic atm, so we will need to handle custom requests.

Ah yes, you would need the ability to use custom requests for some functionality - I do think it'd be better overall to build on DAP than flutter run --machine though - you won't (assuming your DAP client supports them) need to build your own support for things like breakpoints/evaluation, and it'll also protect you from many kinds of SDK changes that DAP has abstracted away.

Waqar144 commented 4 weeks ago

@DanTup

Off the top of my head, I'm wondering if it might be related to structured errors being on by default (which means they're only going over the VM Service).

Ah, that was it. I disabled them by adding the following argument

--dart-define=flutter.inspector.structuredErrors=false

and now everything is reported as expected. Thanks a lot for helping. And I think this issue can be closed.

Overall going the DAP route seems like a more sensible thing to do.

DanTup commented 4 weeks ago

Yep, using DAP this wouldn't be an issue as the debug adapter would have received the structured errors events over the VM Service and converted them to output events.

Reading the comments above, I think this is probably working as intended - I expect that flutter run is printing the errors itself because it's intended to be used by a user in a terminal, but for flutter run --machine, the client running the process is expected to listen for the Flutter.Error events and present them to the user in an appropriate way (which may or may not be in the console).

I'm not sure why it wasn't showing up for @arkanmgerges until they reset the settings in Android Studio, but unless anyone is still seeing this behaviour (the errors not being visible in Android Studio, not that they don't show up if you run --machine manually from a terminal) so it can be debugged further, I suggest we close it (@danagbemava-nc ?)

danagbemava-nc commented 4 weeks ago

I'll be closing this based on https://github.com/flutter/flutter/issues/92903#issuecomment-2372022097. If there's anything more to report, please leave a comment below and I will reopen this.

Thank you

github-actions[bot] commented 1 week ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.