( 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

ToolTypeBest For
Firebase CrashlyticsCrash reportingReal-time crash tracking, free
SentryError monitoringDetailed stack traces, multi-platform
Firebase AnalyticsUser analyticsUser events, funnels, retention
DatadogFull observabilityEnterprise-scale monitoring

1. Firebase Crashlytics

Setup

yaml
dependencies:
  firebase_core: ^2.x.x
  firebase_crashlytics: ^3.x.x
dart
void 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

yaml
dependencies:
  sentry_flutter: ^7.x.x
dart
import '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

dart
try {
  await riskyOperation();
} catch (exception, stackTrace) {
  await Sentry.captureException(
    exception,
    stackTrace: stackTrace,
  );
}

3. Global Exception Handling

dart
void 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

FeatureFirebase CrashlyticsSentry
Free TierYes (unlimited)Yes (5K errors/month)
Real-time alerts
Stack trace grouping
Breadcrumbs
Performance monitoringWith Firebase Performance✅ Built-in
Platform supportiOS, Android, FlutterMulti-platform

Tip: Use Firebase Crashlytics for crash reporting and Sentry for detailed error monitoring with breadcrumbs and user context for the best coverage.