Report errors to a service
While one always tries to create apps that are free of bugs, they're sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, it's important to understand how often your users experience 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 error occurs, create a report containing the error that occurred and the associated stacktrace. You can then send the report to an error tracking service, such as Bugsnag, Datadog, Firebase Crashlytics, Rollbar, or Sentry.
The error tracking service aggregates all of the crashes your users experience and groups them together. This allows you to know how often your app fails and where the users run into trouble.
In this recipe, learn how to report errors to the Sentry crash reporting service using the following steps:
- Get a DSN from Sentry.
- Import the Flutter Sentry package
- Initialize the Sentry SDK
- Capture errors programmatically
1. Get a DSN from Sentry
#Before reporting errors to Sentry, you need a "DSN" to uniquely identify your app with the Sentry.io service.
To get a DSN, use the following steps:
- Create an account with Sentry.
- Log in to the account.
- Create a new Flutter project.
- Copy the code snippet that includes the DSN.
2. Import the Sentry package
#Import the sentry_flutter
package into the app. The sentry package makes it easier to send error reports to the Sentry error tracking service.
To add the sentry_flutter
package as a dependency, run flutter pub add
:
flutter pub add sentry_flutter
3. Initialize the Sentry SDK
#Initialize the SDK to capture different unhandled errors automatically:
import 'package:flutter/widgets.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
Future<void> main() async {
await SentryFlutter.init(
(options) => options.dsn = 'https://[email protected]/example',
appRunner: () => runApp(const MyApp()),
);
}
Alternatively, you can pass the DSN to Flutter using the dart-define
tag:
--dart-define SENTRY_DSN=https://[email protected]/example
What does that give me?
#This is all you need for Sentry to capture unhandled errors in Dart and native layers.
This includes Swift, Objective-C, C, and C++ on iOS, and Java, Kotlin, C, and C++ on Android.
4. Capture errors programmatically
#Besides the automatic error reporting that Sentry generates by importing and initializing the SDK, you can use the API to report errors to Sentry:
await Sentry.captureException(exception, stackTrace: stackTrace);
For more information, see the Sentry API docs on pub.dev.
Mehr erfahren
#Extensive documentation about using the Sentry SDK can be found on Sentry's site.
Complete example
#To view a working example, see the Sentry flutter example app.
Unless stated otherwise, the documentation on this site reflects the latest stable version of Flutter. Page last updated on 2024-04-06. View source or report an issue.