Flutter中添加Sentry的正确方法

use*_*968 3 sentry flutter

来自https://flutter.dev/docs/cookbook/maintenance/error-reporting

runZoned<Future<void>>(() async {
  runApp(CrashyApp());
}, onError: (error, stackTrace) {
  // Whenever an error occurs, call the `_reportError` function. This sends
  // Dart errors to the dev console or Sentry depending on the environment.
  _reportError(error, stackTrace);
});
Run Code Online (Sandbox Code Playgroud)

但我的 IDE 说onError已弃用。

在此输入图像描述

解决这个问题的正确方法是什么?我无法提供有关 runZonedGuarded 的任何示例。

jon*_*jon 5

这是对我有用的设置:

Future<void> main() async {
  final sentry = Sentry.SentryClient(Sentry.SentryOptions(dsn: '[Add dsn URI here]'));

  runZonedGuarded(() {
    WidgetsFlutterBinding.ensureInitialized();

    FlutterError.onError = (FlutterErrorDetails errorDetails) {
      sentry.captureException(
        errorDetails.exception,
        stackTrace: errorDetails.stack,
      );
    };

    runApp(MyApp());
  }, (Object error, StackTrace stackTrace) {
    sentry.captureException(
      error,
      stackTrace: stackTrace,
    );
  });
}
Run Code Online (Sandbox Code Playgroud)