Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WidgetsFlutterBinding.ensureInitialized(); must be called inside zone #95

Open
passsy opened this issue Oct 28, 2019 · 2 comments
Open

Comments

@passsy
Copy link

passsy commented Oct 28, 2019

I had trouble getting errors reported. Synchronous errors where caught but not asynchronous ones. The problem was that WidgetsFlutterBinding.ensureInitialized() was called outside of the error catching zone.

// doesn't work
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  FlutterError.onError = (FlutterErrorDetails details) {
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };

  await FlutterCrashlytics().initialize();

  runZoned<Future<Null>>(() async {
    runApp(MyApp());
  }, onError: (error, stackTrace) async {
    await FlutterCrashlytics().reportCrash(error, stackTrace, forceCrash: false);
  });
}

Moving WidgetsFlutterBinding.ensureInitialized() inside runZoned fixes it and now reports all errors.

// works
void main() async {
  FlutterError.onError = (FlutterErrorDetails details) {
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };

  runZoned<Future<Null>>(() async {
    WidgetsFlutterBinding.ensureInitialized();
    await FlutterCrashlytics().initialize();
    runApp(MyApp());
  }, onError: (error, stackTrace) async {
    await FlutterCrashlytics().reportCrash(error, stackTrace, forceCrash: false);
  });
}

I think the Readme should get an update.

@jaumard
Copy link
Contributor

jaumard commented Oct 28, 2019

Hey @passsy thanks for the report, look super weird !! because from the doc https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html once it's called it's done, so it shouldn't mind having it outside the zone.

When I have some time I'll update the readme, keep this one opened until it's done :)

@linxuebin1990
Copy link

Also can see https://docs.flutter.dev/testing/errors#errors-not-caught-by-flutter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants