flutter_crash_guard
Error handling mixin and Firebase Crashlytics integration. Works with any state management or DI—implement one getter and call handleError.
Features
- ErrorHandlingMixin: Add the mixin, implement
ErrorHandlingService get errorHandlingService(from Riverpod, GetIt, etc.), then callhandleError(operation: '...', error: e, ...). - ErrorHandlingService: Wraps Firebase Crashlytics (init, log, recordError, Flutter/platform/zoned error handlers).
- ErrorSeverity: low, medium, high, critical (mapped from error type when not overridden).
- Optional CrashlyticsNavigatorObserver for route breadcrumbs.
- Riverpod:
errorHandlingServiceProviderandref.errorHandlingServiceextension for the getter.
Installation
Add to pubspec.yaml:
dependencies:
flutter_crash_guard: ^2.0.0
Ensure your app has Firebase configured (e.g. Firebase.initializeApp and Crashlytics in your project).
Setup
- Create and initialize the service, then wire it to your DI and Flutter/platform handlers:
final errorHandlingService = ErrorHandlingService();
await errorHandlingService.initialize(firebaseOptions, enableInDebugMode: false);
FlutterError.onError = errorHandlingService.handleFlutterError;
PlatformDispatcher.instance.onError = errorHandlingService.handlePlatformError;
// In runZonedGuarded, use errorHandlingService.handleZonedError for the zone callback.
// Riverpod: override the provider in your ProviderContainer:
// errorHandlingServiceProvider.overrideWithValue(errorHandlingService),
// Other DI: register errorHandlingService (GetIt, Provider, Bloc constructor, Get.put, etc.).
- Use the mixin by implementing the
errorHandlingServicegetter and callinghandleErrorwithout a ref:
// Riverpod example
class MyNotifier extends StateNotifier<MyState> with ErrorHandlingMixin {
MyNotifier(this.ref) : super(MyState.initial());
final Ref ref;
@override
ErrorHandlingService get errorHandlingService => ref.errorHandlingService;
Future<void> load() async {
try {
// ...
} catch (e, s) {
handleError(operation: 'load', error: e, stackTrace: s);
}
}
}
Other state management
Implement ErrorHandlingService get errorHandlingService with your DI and call handleError(operation: ..., error: e, stackTrace: s).
- Provider: In a class that has access to
BuildContext, e.g. store the service in a field fromcontext.read<ErrorHandlingService>()ininitState/didChangeDependencies, thenErrorHandlingService get errorHandlingService => _service;. - GetIt:
ErrorHandlingService get errorHandlingService => GetIt.I<ErrorHandlingService>(); - Bloc: Inject
ErrorHandlingServicein the bloc constructor; thenErrorHandlingService get errorHandlingService => _errorHandlingService; - GetX:
ErrorHandlingService get errorHandlingService => Get.find<ErrorHandlingService>();(after registering withGet.put).
License
BSD-3-Clause. See LICENSE.