6

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.

Pedro Massango
  • 4,114
  • 2
  • 28
  • 48
Void
  • 1,129
  • 1
  • 8
  • 21

3 Answers3

8

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);
  });
}
Pedro Massango
  • 4,114
  • 2
  • 28
  • 48
  • Thanks! I will try it when I will get to work.Do you know how to you find the library that causes the error? Just.in case I will run into a similar error in.the future. – Void Jun 17 '23 at 22:07
  • I have implemented what you have suggested, but sadly nothing has changed https://gist.github.com/Turbozanik/8f90fd1e68111f23f3afa8aba91a7e75 – Void Jun 19 '23 at 09:25
  • @void your runApp() needs to be outside of Sentry.init, just like I did in my example above – Pedro Massango Jun 27 '23 at 15:20
0

You can find the solution at @Medium , To solve Zone mismatch error without using external package.

Future<void> main() async {


runZonedGuarded<Future<void>>(() async {

WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
  FlutterError.onError = (FlutterErrorDetails details) {
   FlutterError.presentError(details);
  };
  ErrorWidget.builder = (FlutterErrorDetails details) {
   return Scaffold(
     appBar: AppBar(
       backgroundColor: Colors.red,
       title: const Text('An error occurred'),
     ),
     body: Center(child: Text(details.toString())),
   );
  };
 }, 
}
Madhan
  • 21
  • 4
0

Folks per zone mismatch breaking changes https://docs.flutter.dev/release/breaking-changes/zone-errors#:~:text=Flutter%20requires%20(and%20has%20always,has%20not%20detected%20such%20mismatches.

You cannot run GuardedZones any more for flutter apps as it has been depreciated and also Sentry itself has change to the new form of not being wrapped in GuardedZones.

Fred Grott
  • 3,505
  • 1
  • 23
  • 18