Advanced
Error Tracking in Flutter: Crashlytics / Sentry Integration
· 8 min read
Why You Need Crash Reporting
In development, you see an error in the console the moment it happens. But once an app ships to the store, it runs on thousands of different devices, OS versions and network conditions. A crash a user hits might be in a scenario you never tested, and you won't know about it — until they uninstall the app and leave a bad review. Crash reporting tools catch these errors automatically and report them to you along with the device model, OS version and stack trace.
Setting Up firebase_crashlytics
After adding the firebase_crashlytics package to your Firebase project, you need to route the errors Flutter and Dart catch to Crashlytics right at the start of your app, in main().
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Errors caught by the Flutter framework (build, layout, etc.)
FlutterError.onError = (details) {
FirebaseCrashlytics.instance.recordFlutterFatalError(details);
};
// Errors caught outside Flutter, at the platform/engine level
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
runApp(const MyApp());
}Together, these two handlers cover unhandled errors both inside and outside the widget tree (for example, in an isolate). From then on, if the app crashes, the report is sent to Firebase Console automatically on the next launch.
Sending Errors Manually: recordError
Not every error crashes the app. You may also want to track cases you catch in a try/catch block, where you just show the user an error message and the app keeps running; for that, call recordError with fatal: false.
try {
await siparisiGonder(sepet);
} catch (e, stackTrace) {
await FirebaseCrashlytics.instance.recordError(
e,
stackTrace,
reason: 'Sipariş gönderimi başarısız',
fatal: false,
);
// Show the user a friendly error message
}The reason parameter makes it easier to tell this error apart from others in the Firebase Console's report list. Adding steps before the error with log() (similar to a breadcrumb) helps you understand which user flow the error happened in.
An Alternative: Sentry
Sentry offers a setup similar to Crashlytics (the sentry_flutter package) but stands out with a few extra capabilities: automatic breadcrumb collection (which screens the user visited and which HTTP requests were made before the error), performance monitoring (spotting slow screens with transactions/spans), and more flexible error grouping rules. For small and mid-size projects, Crashlytics' integration with the Firebase ecosystem (especially if you're already using Firebase) is a practical advantage; if performance monitoring and richer breadcrumbs are a priority, Sentry is the stronger option.
await SentryFlutter.init(
(options) {
options.dsn = 'https://ornek@sentry.io/proje-id';
options.tracesSampleRate = 0.2; // 20% of requests are traced
},
appRunner: () => runApp(const MyApp()),
);Things to Watch Out For
1. Never Include Sensitive Data in a Report
Never put passwords, tokens, or credit card numbers into the reason text you give recordError or into breadcrumb logs; these reports go to a dashboard your debugging team can access, and could unintentionally turn into a data leak.
2. Test It in Debug Mode
By default, Crashlytics may not send reports to Firebase Console in debug mode (this varies by platform). When testing the integration, make sure collection is forced on with FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true) and that a test crash actually shows up in the dashboard.
Conclusion
Crash reporting is your most valuable source of feedback after launch. Use FlutterError.onError and PlatformDispatcher.instance.onError to catch global errors, and recordError for anticipated but still worth-tracking cases. If you're already on Firebase, Crashlytics is a practical starting point; if performance monitoring and rich breadcrumbs matter more, consider Sentry. Either way, be careful never to leak sensitive data into the reports.