@@ -14,23 +14,24 @@ import 'package:flutter/material.dart';
14
14
15
15
// Toggle this to cause an async error to be thrown during initialization
16
16
// and to test that runZonedGuarded() catches the error
17
- final _kShouldTestAsyncErrorOnInit = false ;
17
+ const _kShouldTestAsyncErrorOnInit = false ;
18
18
19
19
// Toggle this for testing Crashlytics in your app locally.
20
- final _kTestingCrashlytics = true ;
20
+ const _kTestingCrashlytics = true ;
21
21
22
- main () {
22
+ void main () {
23
23
WidgetsFlutterBinding .ensureInitialized ();
24
24
runZonedGuarded (() {
25
25
runApp (MyApp ());
26
26
}, (error, stackTrace) {
27
- print ('runZonedGuarded: Caught error in my root zone.' );
28
27
FirebaseCrashlytics .instance.recordError (error, stackTrace);
29
28
});
30
29
}
31
30
32
31
// ignore: public_member_api_docs
33
32
class MyApp extends StatefulWidget {
33
+ // ignore: public_member_api_docs
34
+ MyApp ({Key key}) : super (key: key);
34
35
@override
35
36
_MyAppState createState () => _MyAppState ();
36
37
}
@@ -41,6 +42,7 @@ class _MyAppState extends State<MyApp> {
41
42
Future <void > _testAsyncErrorOnInit () async {
42
43
Future <void >.delayed (const Duration (seconds: 2 ), () {
43
44
final List <int > list = < int > [];
45
+ // ignore: avoid_print
44
46
print (list[100 ]);
45
47
});
46
48
}
@@ -100,103 +102,115 @@ class _MyAppState extends State<MyApp> {
100
102
child: Column (
101
103
children: < Widget > [
102
104
RaisedButton (
103
- child: const Text ('Key' ),
104
- onPressed: () {
105
- FirebaseCrashlytics .instance
106
- .setCustomKey ('example' , 'flutterfire' );
107
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
108
- content: Text (
109
- 'Custom Key "example: flutterfire" has been set \n '
110
- 'Key will appear in Firebase Console once app has crashed and reopened' ),
111
- duration: Duration (seconds: 5 ),
112
- ));
113
- }),
105
+ onPressed: () {
106
+ FirebaseCrashlytics .instance
107
+ .setCustomKey ('example' , 'flutterfire' );
108
+ ScaffoldMessenger .of (context)
109
+ .showSnackBar (const SnackBar (
110
+ content: Text (
111
+ 'Custom Key "example: flutterfire" has been set \n '
112
+ 'Key will appear in Firebase Console once app has crashed and reopened' ),
113
+ duration: Duration (seconds: 5 ),
114
+ ));
115
+ },
116
+ child: const Text ('Key' ),
117
+ ),
114
118
RaisedButton (
115
- child: const Text ('Log' ),
116
- onPressed: () {
117
- FirebaseCrashlytics .instance
118
- .log ('This is a log example' );
119
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
120
- content: Text (
121
- 'The message "This is a log example" has been logged \n '
122
- 'Message will appear in Firebase Console once app has crashed and reopened' ),
123
- duration: Duration (seconds: 5 ),
124
- ));
125
- }),
119
+ onPressed: () {
120
+ FirebaseCrashlytics .instance
121
+ .log ('This is a log example' );
122
+ ScaffoldMessenger .of (context)
123
+ .showSnackBar (const SnackBar (
124
+ content: Text (
125
+ 'The message "This is a log example" has been logged \n '
126
+ 'Message will appear in Firebase Console once app has crashed and reopened' ),
127
+ duration: Duration (seconds: 5 ),
128
+ ));
129
+ },
130
+ child: const Text ('Log' ),
131
+ ),
126
132
RaisedButton (
127
- child: const Text ('Crash' ),
128
- onPressed: () async {
129
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
130
- content: Text ('App will crash is 5 seconds \n '
131
- 'Please reopen to send data to Crashlytics' ),
132
- duration: Duration (seconds: 5 ),
133
- ));
134
-
135
- // Delay crash for 5 seconds
136
- sleep (const Duration (seconds: 5 ));
137
-
138
- // Use FirebaseCrashlytics to throw an error. Use this for
139
- // confirmation that errors are being correctly reported.
140
- FirebaseCrashlytics .instance.crash ();
141
- }),
133
+ onPressed: () async {
134
+ ScaffoldMessenger .of (context)
135
+ .showSnackBar (const SnackBar (
136
+ content: Text ('App will crash is 5 seconds \n '
137
+ 'Please reopen to send data to Crashlytics' ),
138
+ duration: Duration (seconds: 5 ),
139
+ ));
140
+
141
+ // Delay crash for 5 seconds
142
+ sleep (const Duration (seconds: 5 ));
143
+
144
+ // Use FirebaseCrashlytics to throw an error. Use this for
145
+ // confirmation that errors are being correctly reported.
146
+ FirebaseCrashlytics .instance.crash ();
147
+ },
148
+ child: const Text ('Crash' ),
149
+ ),
142
150
RaisedButton (
143
- child: const Text ('Throw Error' ),
144
- onPressed: () {
145
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
146
- content: Text ('Thrown error has been caught \n '
147
- 'Please crash and reopen to send data to Crashlytics' ),
148
- duration: Duration (seconds: 5 ),
149
- ));
150
-
151
- // Example of thrown error, it will be caught and sent to
152
- // Crashlytics.
153
- throw StateError ('Uncaught error thrown by app' );
154
- }),
151
+ onPressed: () {
152
+ ScaffoldMessenger .of (context)
153
+ .showSnackBar (const SnackBar (
154
+ content: Text ('Thrown error has been caught \n '
155
+ 'Please crash and reopen to send data to Crashlytics' ),
156
+ duration: Duration (seconds: 5 ),
157
+ ));
158
+
159
+ // Example of thrown error, it will be caught and sent to
160
+ // Crashlytics.
161
+ throw StateError ('Uncaught error thrown by app' );
162
+ },
163
+ child: const Text ('Throw Error' ),
164
+ ),
155
165
RaisedButton (
156
- child: const Text ('Async out of bounds' ),
157
- onPressed: () {
158
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
159
- content: Text (
160
- 'Uncaught Exception that is handled by second parameter of runZonedGuarded \n '
166
+ onPressed: () {
167
+ ScaffoldMessenger .of (context)
168
+ .showSnackBar (const SnackBar (
169
+ content: Text (
170
+ 'Uncaught Exception that is handled by second parameter of runZonedGuarded \n '
171
+ 'Please crash and reopen to send data to Crashlytics' ),
172
+ duration: Duration (seconds: 5 ),
173
+ ));
174
+
175
+ // Example of an exception that does not get caught
176
+ // by `FlutterError.onError` but is caught by
177
+ // `runZonedGuarded`.
178
+ runZonedGuarded (() {
179
+ Future <void >.delayed (const Duration (seconds: 2 ),
180
+ () {
181
+ final List <int > list = < int > [];
182
+ // ignore: avoid_print
183
+ print (list[100 ]);
184
+ });
185
+ }, FirebaseCrashlytics .instance.recordError);
186
+ },
187
+ child: const Text ('Async out of bounds' ),
188
+ ),
189
+ RaisedButton (
190
+ onPressed: () async {
191
+ try {
192
+ ScaffoldMessenger .of (context)
193
+ .showSnackBar (const SnackBar (
194
+ content: Text ('Recorded Error \n '
161
195
'Please crash and reopen to send data to Crashlytics' ),
162
196
duration: Duration (seconds: 5 ),
163
197
));
164
-
165
- // Example of an exception that does not get caught
166
- // by `FlutterError.onError` but is caught by
167
- // `runZonedGuarded`.
168
- runZonedGuarded (() {
169
- Future <void >.delayed (const Duration (seconds: 2 ),
170
- () {
171
- final List <int > list = < int > [];
172
- print (list[100 ]);
173
- });
174
- }, FirebaseCrashlytics .instance.recordError);
175
- }),
176
- RaisedButton (
177
- child: const Text ('Record Error' ),
178
- onPressed: () async {
179
- try {
180
- ScaffoldMessenger .of (context)
181
- .showSnackBar (SnackBar (
182
- content: Text ('Recorded Error \n '
183
- 'Please crash and reopen to send data to Crashlytics' ),
184
- duration: Duration (seconds: 5 ),
185
- ));
186
- throw 'error_example' ;
187
- } catch (e, s) {
188
- // "reason" will append the word "thrown" in the
189
- // Crashlytics console.
190
- await FirebaseCrashlytics .instance
191
- .recordError (e, s, reason: 'as an example' );
192
- }
193
- }),
198
+ throw Error ();
199
+ } catch (e, s) {
200
+ // "reason" will append the word "thrown" in the
201
+ // Crashlytics console.
202
+ await FirebaseCrashlytics .instance
203
+ .recordError (e, s, reason: 'as an example' );
204
+ }
205
+ },
206
+ child: const Text ('Record Error' ),
207
+ ),
194
208
],
195
209
),
196
210
);
197
211
break ;
198
212
default :
199
- return Center (child: Text ('Loading' ));
213
+ return const Center (child: Text ('Loading' ));
200
214
}
201
215
},
202
216
),
0 commit comments