如何修复此哨兵区不匹配错误?

2024-01-09

框架/SDK版本:

Flutter: 3.10.4
Dart: 3.0.3

这是我的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(),
              ),
            ),
          ));
}

我收到以下错误,但无法找到:

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>
=================================================================================================

有人能够摆脱这个吗?任何建议表示赞赏。


您可以在以下位置找到解决方案https://github.com/getsentry/sentry-dart/tree/main/flutter#usage https://github.com/getsentry/sentry-dart/tree/main/flutter#usage.

ensureInitialized必须在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://[email protected] /cdn-cgi/l/email-protection/add-your-dsn-here';
      },
    );
    // or here
    runApp(MyApp());
  }, (exception, stackTrace) async {
    await Sentry.captureException(exception, stackTrace: stackTrace);
  });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复此哨兵区不匹配错误? 的相关文章

随机推荐