( Crash Analytics , Exception , error) handling (like firebase analytics , Sentry)
#firebase
Answer
Overview
Crash analytics and exception handling are critical for maintaining app quality. They help you detect, report, and resolve errors before users are significantly impacted.
Key Tools
| Tool | Type | Best For |
|---|---|---|
| Firebase Crashlytics | Crash reporting | Real-time crash tracking, free |
| Sentry | Error monitoring | Detailed stack traces, multi-platform |
| Firebase Analytics | User analytics | User events, funnels, retention |
| Datadog | Full observability | Enterprise-scale monitoring |
1. Firebase Crashlytics
Setup
yamldependencies: firebase_core: ^2.x.x firebase_crashlytics: ^3.x.x
dartvoid main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); // Catch Flutter framework errors FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError; // Catch async errors outside Flutter PlatformDispatcher.instance.onError = (error, stack) { FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); return true; }; runApp(MyApp()); }
Log Custom Errors
dart// Non-fatal error try { await fetchData(); } catch (e, stackTrace) { await FirebaseCrashlytics.instance.recordError( e, stackTrace, reason: 'Failed to fetch data', fatal: false, ); } // Add custom keys for context FirebaseCrashlytics.instance.setCustomKey('user_id', userId); FirebaseCrashlytics.instance.setCustomKey('screen', 'HomeScreen'); // Add custom log message FirebaseCrashlytics.instance.log('User tapped checkout button');
2. Sentry
Setup
yamldependencies: sentry_flutter: ^7.x.x
dartimport 'package:sentry_flutter/sentry_flutter.dart'; Future<void> main() async { await SentryFlutter.init( (options) { options.dsn = 'YOUR_SENTRY_DSN'; options.tracesSampleRate = 1.0; options.environment = 'production'; }, appRunner: () => runApp(MyApp()), ); }
Capture Exceptions
darttry { await riskyOperation(); } catch (exception, stackTrace) { await Sentry.captureException( exception, stackTrace: stackTrace, ); }
3. Global Exception Handling
dartvoid main() { // Catch all uncaught sync errors runZonedGuarded( () => runApp(MyApp()), (error, stackTrace) { // Log to Crashlytics or Sentry FirebaseCrashlytics.instance.recordError(error, stackTrace); }, ); }
Best Practices
- Always catch and report exceptions in async operations
- Add context (user ID, screen name, action) before the crash
- Distinguish fatal vs non-fatal errors for proper prioritization
- Test crash reporting before going to production
- Monitor crash-free rate — aim for 99.5%+
- Set up alerts for sudden spikes in crash rates
Quick Comparison
| Feature | Firebase Crashlytics | Sentry |
|---|---|---|
| Free Tier | Yes (unlimited) | Yes (5K errors/month) |
| Real-time alerts | ✅ | ✅ |
| Stack trace grouping | ✅ | ✅ |
| Breadcrumbs | ✅ | ✅ |
| Performance monitoring | With Firebase Performance | ✅ Built-in |
| Platform support | iOS, Android, Flutter | Multi-platform |
Tip: Use Firebase Crashlytics for crash reporting and Sentry for detailed error monitoring with breadcrumbs and user context for the best coverage.