【问题标题】:Corret way to add Sentry to Flutter将哨兵添加到 Flutter 的正确方法
【发布时间】:2020-05-11 23:26:47
【问题描述】:

来自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);
});

但我的 IDE 说 onError 已弃用。

解决此问题的正确方法是什么?我在 runZonedGuarded 上没有任何示例。

【问题讨论】:

    标签: flutter sentry


    【解决方案1】:

    这是对我有用的设置:

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

    【讨论】:

      【解决方案2】:

      在 Flutter 1.17.0 (It's a breaking change from Dart 2.8) 上看起来像 became obsolete。你可以这样:

      
      runZonedGuarded(() {
        runApp(CrashyApp());
      }, (Object error, StackTrace 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);
      });
      

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        • 2021-09-29
        • 1970-01-01
        • 2018-03-07
        • 2021-08-10
        • 1970-01-01
        相关资源
        最近更新 更多