flutterdartsentrydart-isolates

How do I fix this Sentry Zone mismatch error?


Framework / SDK versions:

Flutter: 3.10.4
Dart: 3.0.3

Here goes my main() code:

Future<void> main() async {
  //debugPaintSizeEnabled = true;
  //BindingBase.debugZoneErrorsAreFatal = true;
  WidgetsFlutterBinding.ensureInitialized();
  EasyLocalization.ensureInitialized()
      .then((value) => Fimber.plantTree(DebugTree()))
      .then((value) => SentryFlutter.init(
            (options) {
              options.dsn = '***';
              // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
              // We recommend adjusting this value in production.
              options.tracesSampleRate = 1.0;
              //options.attachScreenshot = true;
            },
            appRunner: () => runApp(
              EasyLocalization(
                supportedLocales: const [Locale('en', 'US'), Locale('de', 'DE')],
                path: '../assets/translations/',
                fallbackLocale: const Locale('en', 'US'),
                assetLoader: const CodegenLoader(),
                child: MyApp(),
              ),
            ),
          ));
}

And I am getting the following error, that I can't locate:

Exception caught by Flutter framework =====================================================
The following assertion was thrown during runApp:
Zone mismatch.

The Flutter bindings were initialized in a different zone than is now being used. This will likely cause confusion and bugs as any zone-specific configuration will inconsistently use the configuration of the original binding initialization zone or this zone based on hard-to-predict factors such as which zone was active when a particular callback was set.
It is important to use the same zone when calling `ensureInitialized` on the binding as when calling `runApp` later.
To make this warning fatal, set BindingBase.debugZoneErrorsAreFatal to true before the bindings are initialized (i.e. as the first statement in `void main() { }`).
When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 942:28   get current
packages/flutter/src/foundation/binding.dart 497:29                  <fn>
packages/flutter/src/foundation/binding.dart 501:14                  debugCheckZone
packages/flutter/src/widgets/binding.dart 1080:17                    runApp
packages/ens_price_calculator/main.dart 52:30                        <fn>
packages/sentry/src/sentry.dart 136:26                               <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50   <fn>
dart-sdk/lib/async/zone.dart 1407:47                                 _rootRunUnary
dart-sdk/lib/async/zone.dart 1308:19                                 runUnary
dart-sdk/lib/async/future_impl.dart 147:18                           handleValue
dart-sdk/lib/async/future_impl.dart 784:44                           handleValueCallback
dart-sdk/lib/async/future_impl.dart 813:13                           _propagateToListeners
dart-sdk/lib/async/future_impl.dart 584:5                            [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 657:7                            <fn>
dart-sdk/lib/async/zone.dart 1399:13                                 _rootRun
dart-sdk/lib/async/zone.dart 1301:19                                 run
dart-sdk/lib/async/zone.dart 1209:7                                  runGuarded
dart-sdk/lib/async/zone.dart 1249:23                                 callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                     _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                      _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 177:15  <fn>
=================================================================================================

Has anyone been able to get rid of this? Any suggestions appreciated.


Solution

  • You can find the solution at https://github.com/getsentry/sentry-dart/tree/main/flutter#usage.

    ensureInitialized has to be called within the runZonedGuarded

    import 'dart:async';
    
    import 'package:flutter/widgets.dart';
    import 'package:sentry_flutter/sentry_flutter.dart';
    
    Future<void> main() async {
      // creates a zone
      await runZonedGuarded(() async {
        WidgetsFlutterBinding.ensureInitialized();
        // Initialize other stuff here...
    
        await SentryFlutter.init(
          (options) {
            options.dsn = 'https://example@sentry.io/add-your-dsn-here';
          },
        );
        // or here
        runApp(MyApp());
      }, (exception, stackTrace) async {
        await Sentry.captureException(exception, stackTrace: stackTrace);
      });
    }