Crashlytics 和 runZonedGuarded

amo*_*new 2 dart firebase crashlytics flutter

我无法理解 runZonedGuarded 用法的 crashlytics 文档

那我到底需要还是不需要呢?

https://firebase.flutter.dev/docs/crashlytics/usage/#zoned-errors

Joh*_*rug 10

2023 年 6 月答复

不,您不再需要runZonedGuardedCrashlytics。

不推荐使用runZonedGuardedCrashlytics。您可以看到在此提交中所有引用都runZonedGuarded被替换:

https://github.com/firebase/flutterfire/commit/8a0caa05d5abf6fef5bf0e654654dcd0b6ec874a

另请注意,当前的官方文档不再提及runZonedGuardedhttps://firebase.google.com/docs/crashlytics/customize-crash-reports ?platform=flutter

推荐的方式是这样的:

Future<void> main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    FlutterError.onError = (errorDetails) {
      FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
    };
    // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
    PlatformDispatcher.instance.onError = (error, stack) {
      FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
      return true;
    };
    runApp(MyApp());

}
Run Code Online (Sandbox Code Playgroud)

享受!