While one always tries to create apps that are free of bugs, they’re sureto crop up from time to time. Since buggy apps lead to unhappyusers and customers, it’s important to understand how often your usersexperience bugs and where those bugs occur. That way,you can prioritize the bugs with the highest impact and work to fix them.

How can you determine how often your users experiences bugs? Whenever an erroroccurs, create a report containing the error that occurred and theassociated stacktrace. You can then send the report to an error trackingservice, such as Sentry, Fabric, or Rollbar.

The error tracking service aggregates all of the crashes your usersexperience and groups them together. This allows you to know how often yourapp fails and where the users run into trouble.

In this recipe, you’ll see how to report errors to theSentry crash reporting service.

Directions

  • Get a DSN from Sentry
  • Import the Sentry package
  • Create a SentryClient
  • Create a function to report errors
  • Catch and report Dart errors
  • Catch and report Flutter errors

1. Get a DSN from Sentry

Before reporting errors to Sentry, you’ll need a “DSN” to uniquely identifyyour app with the Sentry.io service.

To get a DSN, use the following steps:

2. Import the Sentry package

Import thesentry package into the app. Thesentry package makes it easier to send error reports to the Sentryerror tracking service.

  1. dependencies:
  2. sentry: <latest_version>

3. Create a SentryClient

Create a SentryClient. You’ll use the SentryClient to senderror reports to the sentry service.

  1. final SentryClient _sentry = SentryClient(dsn: "App DSN goes Here");

4. Create a function to report errors

With Sentry set up, you can begin to report errors. Since you don’t want toreport errors to Sentry during development, first create a function thatlets you know whether you’re in debug or production mode.

  1. bool get isInDebugMode {
  2. // Assume you're in production mode
  3. bool inDebugMode = false;
  4. // Assert expressions are only evaluated during development. They are ignored
  5. // in production. Therefore, this code only sets `inDebugMode` to true
  6. // in a development environment.
  7. assert(inDebugMode = true);
  8. return inDebugMode;
  9. }

Next, use this function in combination with the SentryClient to reporterrors when the app is in production mode.

  1. Future<void> _reportError(dynamic error, dynamic stackTrace) async {
  2. // Print the exception to the console
  3. print('Caught error: $error');
  4. if (isInDebugMode) {
  5. // Print the full stacktrace in debug mode
  6. print(stackTrace);
  7. return;
  8. } else {
  9. // Send the Exception and Stacktrace to Sentry in Production mode
  10. _sentry.captureException(
  11. exception: error,
  12. stackTrace: stackTrace,
  13. );
  14. }
  15. }

5. Catch and report Dart errors

Now that you have a function to report errors depending on the environment,you need a way to capture Dart errors.

For this task, run your app inside a customZone. Zonesestablish an execution context for the code. This provides a convenient way tocapture all errors that occur within that context by providing an onErrorfunction.

In this case, you’ll run the app in a new Zone and capture all errors byproviding an onError callback.

  1. runZoned<Future<void>>(() async {
  2. runApp(CrashyApp());
  3. }, onError: (error, stackTrace) {
  4. // Whenever an error occurs, call the `_reportError` function. This sends
  5. // Dart errors to the dev console or Sentry depending on the environment.
  6. _reportError(error, stackTrace);
  7. });

6. Catch and report Flutter errors

In addition to Dart errors, Flutter can throw additional errors, such asplatform exceptions that occur when calling native code. You need to be sure tocapture and report these types of errors as well.

To capture Flutter errors, override theFlutterError.onErrorproperty. If you’re in debug mode, use a convenience functionfrom Flutter to properly format the error. If you’re in production mode, send the error to the onError callback defined in the previous step.

  1. // This captures errors reported by the Flutter framework.
  2. FlutterError.onError = (FlutterErrorDetails details) {
  3. if (isInDebugMode) {
  4. // In development mode, simply print to console.
  5. FlutterError.dumpErrorToConsole(details);
  6. } else {
  7. // In production mode, report to the application zone to report to
  8. // Sentry.
  9. Zone.current.handleUncaughtError(details.exception, details.stack);
  10. }
  11. };

Complete example

To view a working example, see theCrashy example app.