@@ -20,57 +20,62 @@ to disk to be sent along with the next fatal report or when the app restarts.
20
20
21
21
## Report uncaught exceptions {: #report-uncaught-exceptions}
22
22
23
- You can automatically catch all errors that are thrown within the Flutter
23
+ You can automatically catch all "fatal" errors that are thrown within the Flutter
24
24
framework by overriding ` FlutterError.onError ` with
25
- ` FirebaseCrashlytics.instance.recordFlutterFatalError ` :
25
+ ` FirebaseCrashlytics.instance.recordFlutterFatalError ` . Alternatively,
26
+ to also catch "non-fatal" exceptions, override ` FlutterError.onError ` with ` FirebaseCrashlytics.instance.recordFlutterError ` :
26
27
27
28
``` dart
28
29
void main() async {
29
30
WidgetsFlutterBinding.ensureInitialized();
30
31
31
32
await Firebase.initializeApp();
32
-
33
- // Pass all uncaught errors from the framework to Crashlytics.
34
- FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
33
+ bool weWantFatalErrorRecording = true;
34
+ FlutterError.onError = (errorDetails) {
35
+ if(weWantFatalErrorRecording){
36
+ FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
37
+ } else {
38
+ FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
39
+ }
40
+ };
35
41
36
42
runApp(MyApp());
37
43
}
38
44
```
39
45
40
- ### Zoned errors {: #zoned-errors}
41
46
42
- Not all errors are caught by Flutter. Sometimes, errors are instead caught by
43
- ` Zones ` . A common case where relying on Flutter to catch errors would not be
44
- enough is when an exception happens inside the ` onPressed ` handler of a button :
47
+ ### Asynchronous errors {: #asynchronous- errors}
48
+
49
+ Asynchronous errors are not caught by the Flutter framework :
45
50
46
51
``` dart
47
52
ElevatedButton(
48
- onPressed: () {
53
+ onPressed: () async {
49
54
throw Error();
50
55
}
51
56
...
52
57
)
53
58
```
54
59
55
- To catch such errors, you can use ` runZonedGuarded ` :
60
+ To catch such errors, you can use the ` PlatformDispatcher.instance.onError ` handler :
56
61
57
62
``` dart
58
- void main() async {
59
- runZonedGuarded<Future<void>>(() async {
63
+ Future<void> main() async {
60
64
WidgetsFlutterBinding.ensureInitialized();
61
65
await Firebase.initializeApp();
62
- // The following lines are the same as previously explained in "Handling uncaught errors"
63
- FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
64
-
66
+ FlutterError.onError = (errorDetails) {
67
+ FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
68
+ };
69
+ // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
70
+ PlatformDispatcher.instance.onError = (error, stack) {
71
+ FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
72
+ return true;
73
+ };
65
74
runApp(MyApp());
66
- }, (error, stack) => FirebaseCrashlytics.instance.recordError(error, stack, fatal: true));
75
+
67
76
}
68
77
```
69
78
70
- Note: You must call ` WidgetsFlutterBinding.ensureInitialized() ` _ inside_
71
- ` runZonedGuarded ` . Error handling wouldn’t work if
72
- ` WidgetsFlutterBinding.ensureInitialized() ` was called from the outside.
73
-
74
79
### Errors outside of Flutter {: #errors-outside-flutter}
75
80
76
81
To catch errors that happen outside of the Flutter context, install an error
@@ -108,6 +113,9 @@ await FirebaseCrashlytics.instance.recordError(
108
113
stackTrace,
109
114
reason: 'a non-fatal error'
110
115
);
116
+
117
+ // Or you can use:
118
+ await FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
111
119
```
112
120
113
121
You may also wish to log further information about the error which is possible
0 commit comments