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

问题描述 投票:0回答:3

框架/SDK版本:

Flutter: 3.10.4
Dart: 3.0.3

这是我的

main()
代码:

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

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

flutter dart sentry dart-isolates
3个回答
8
投票

您可以在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]/add-your-dsn-here';
      },
    );
    // or here
    runApp(MyApp());
  }, (exception, stackTrace) async {
    await Sentry.captureException(exception, stackTrace: stackTrace);
  });
}

0
投票

您可以在@Medium找到解决方案,无需使用外部包即可解决区域不匹配错误。

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())),
   );
  };
 }, 
}

0
投票

每个区域不匹配的人员发生重大变化 https://docs.flutter.dev/release/writing-changes/zone-errors#:~:text=Flutter%20requires%20(并且%20has%20always,has%20not%20Detected%20such%20mismatches。

您无法再为 flutter 应用程序运行 GuardedZones,因为它已被折旧,而且 Sentry 本身也已更改为不包含在 GuardedZones 中的新形式。

© www.soinside.com 2019 - 2024. All rights reserved.