Reputation: 149
I am integrating Crashlytics in my Flutter app and the sample code suggest use of runZonedGuarded. I was trying to understand what it is and how does it effect app stability and performance.
Upvotes: 10
Views: 7623
Reputation: 497
From what I gather, runZonedGuarded is used to catch unhandled errors and exceptions that occur within a specific zone in Dart. Zones are like separate execution contexts that can have their own error handling and asynchronous behavior. By using runZonedGuarded, you can ensure that any uncaught errors in that zone are handled gracefully, which is particularly useful for logging crashes in Crashlytics.
runZonedGuarded creates a zone to catch uncaught errors like async, Flutter framework, and Dart runtime errors, preventing app crashes and enabling controlled error handling. It improves stability by allowing custom recovery strategies and better error tracking through Crashlytics, while having minimal performance overhead. The benefits of enhanced debugging and stability typically outweigh any minor performance costs.
Here’s a basic example of how it might be used:
void main() {
runZonedGuarded(() {
runApp(MyApp());
}, (error, stackTrace) {
FirebaseCrashlytics.instance.recordError(error, stackTrace);
});
}
Upvotes: 0
Reputation: 2506
see link below for detailed explanation on the benefits of runZonedGuarded by Fred Grott. As I understand it, runZonedGuarded essentially provides better traceability for errors, exceptions and debugging... Mastering Flutter Zones
As one example, it can be used as follows:
void main {
runZonedGuarded(
() => runApp(App()),
(error, stackTrace) => log(error.toString(), stackTrace: stackTrace),
);
}
Upvotes: 7