Corret way to add Sentry to Flutter

Viewed 3605

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

But My IDE said onError is decprecated.

enter image description here

What's the proper way to fix this? I can't any example on runZonedGuarded.

2 Answers

This is the setup that worked for me:

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

Looks like it became obsolete on Flutter 1.17.0 (It's a breaking change from Dart 2.8). You could something like this:


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