Corret way to add Sentry to Flutter

3,333

Solution 1

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

Solution 2

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);
});
Share:
3,333
user1187968
Author by

user1187968

Updated on December 20, 2022

Comments

  • user1187968
    user1187968 over 1 year

    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.