DEV Community

Cover image for 🐞 Catch exceptions in Flutter with Catcher & Sentry
Chandu J S
Chandu J S

Posted on • Updated on • Originally published at chandujs.dev

🐞 Catch exceptions in Flutter with Catcher & Sentry

Who doesn't hate unhandled exceptions in their applications?

In this post, I will show you how to catch unhandled exceptions in a flutter application using a Catcher plugin and sentry.io

Create a new project in Sentry

Go to sentry.io and create a new project

Create New Project

We can just use the DSN for our application.

Get DSN

Install Catcher

Open your app's pubspec.yml file and add catcher as a dependency.

dependencies:
  catcher: ^0.2.7
Enter fullscreen mode Exit fullscreen mode

NOTE: Catcher version mentioned in this post may not be the latest version.

And in your main.dart file,

Import the package,

import 'package:catcher/catcher_plugin.dart';
Enter fullscreen mode Exit fullscreen mode

Replace

runApp(App());
Enter fullscreen mode Exit fullscreen mode

with

void main() {
  /// checking if release mode or not
  /// no need of Catcher & Sentry in debug mode.
  if (kReleaseMode) {
    Catcher(
      App(),
      releaseConfig: CatcherOptions(
        SilentReportMode(),
        [SentryHandler('<SENTRY_DSN_STRING>')],
      ),
    );
  } else {
    runApp(App());
  }
}
Enter fullscreen mode Exit fullscreen mode

Replace your sentry DSN with <SENTRY_DSN_STRING> in SentryHandler

In CatcherOptions, we have to provide a ReportMode as the first parameter and at least one ReportHandler as the second parameter. Here we are using SilentReportMode() as ReportMode and SentryHandler() as error handler.

What we are expecting here is, if there is any unhandled exception occurs, the app will send the error report to sentry silently. If you want to get user's confirmation to send an error report, you can use DialogReportMode() instead of SilentReportMode().

You can refer more in the plugin documentation here

next, in MaterialApp widget add Catcher.navigatorKey as value of navigatorKey property

class App extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: Catcher.navigatorKey, // <---------
      home: RootPage(),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

And that's it!

Catcher will catch all the unhandled exceptions in the application from now on. You can view the exceptions in the sentry dashboard.

Top comments (0)