Sentry SDK for Dart and Flutter

Usage

Sign up for a Sentry.io account and get a DSN at http://sentry.io.

In your Dart code, import package:sentry/sentry.dart and initialize the Sentry SDK using the DSN issued by Sentry.io:

import 'package:sentry/sentry.dart';

Sentry.init((options) => options.dsn = 'https://example@sentry.io/add-your-dsn-here');

In an exception handler, call captureException():

import 'dart:async';
import 'package:sentry/sentry.dart';

void main() async {
  try {
    aMethodThatMightFail();
  } catch (exception, stackTrace) {
    await Sentry.captureException(
      exception,
      stackTrace: stackTrace,
    );
  }
}
Tips for catching errors
  • Use a try/catch block, like in the example above.
  • Create a Zone with an error handler, e.g. using runZonedGuarded.
import 'dart:async';

import 'package:flutter/material.dart';

import 'package:sentry/sentry.dart';

// Wrap your 'runApp(MyApp())' as follows:

Future<void> main() async {
  runZonedGuarded<Future<void>>(() async {
    runApp(MyApp());
  }, (exception, stackTrace) async {
    await Sentry.captureException(
      exception,
      stackTrace: stackTrace,
    );
  });
}
  • For Flutter-specific errors (such as layout failures), use FlutterError.onError. For example:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sentry/sentry.dart';

// Wrap your 'runApp(MyApp())' as follows:

Future<void> main() async {
  FlutterError.onError = (FlutterErrorDetails details) async {
    await Sentry.captureException(
      details.exception,
      stackTrace: details.stack,
    );
  };
}
  • Use Isolate.current.addErrorListener to capture uncaught errors in the root zone.

Resources

  • Documentation
  • Forum
  • Discord
  • Stack Overflow
  • Twitter Follow

Libraries

sentry
A pure Dart client for Sentry.io crash reporting.