diff --git a/docs/crashlytics/_customize-crash-reports.md b/docs/crashlytics/_customize-crash-reports.md index 127e89081a6c..6a4331349656 100644 --- a/docs/crashlytics/_customize-crash-reports.md +++ b/docs/crashlytics/_customize-crash-reports.md @@ -23,20 +23,25 @@ to disk to be sent along with the next fatal report or when the app restarts. You can automatically catch all "fatal" errors that are thrown within the Flutter framework by overriding `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterFatalError`. Alternatively, -to also catch "non-fatal" exceptions, override `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterError`: +to catch exceptions as "non-fatal" instead, override `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterError`: ```dart void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); - bool weWantFatalErrorRecording = true; FlutterError.onError = (errorDetails) { - if(weWantFatalErrorRecording){ - FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails); - } else { - FirebaseCrashlytics.instance.recordFlutterError(errorDetails); - } + // If you want to record a "non-fatal" exception, use `FirebaseCrashlytics.instance.recordFlutterError` instead + const fatalError = true; + FlutterError.onError = (errorDetails) { + if (fatalError) { + // If you want to record a "fatal" exception + FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails); + } else { + // If you want to record a "non-fatal" exception + FirebaseCrashlytics.instance.recordFlutterError(errorDetails); + } + }; }; runApp(MyApp()); @@ -67,8 +72,15 @@ Future main() async { FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails); }; // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics + const fatalError = true; PlatformDispatcher.instance.onError = (error, stack) { - FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); + if (fatalError) { + // If you want to record a "fatal" exception + FirebaseCrashlytics.instance.recordError(error, stack, fatal: fatalError); + } else { + // If you want to record a "non-fatal" exception + FirebaseCrashlytics.instance.recordError(error, stack); + } return true; }; runApp(MyApp()); @@ -118,7 +130,7 @@ await FirebaseCrashlytics.instance.recordError( await FirebaseCrashlytics.instance.recordFlutterError(errorDetails); ``` -You may also wish to log further information about the error which is possible +You may also want to log further information about the error which is possible using the `information` property: ```dart diff --git a/docs/crashlytics/_force-test-crash.md b/docs/crashlytics/_force-test-crash.md index 6362693c21d4..13c2956ecd0b 100644 --- a/docs/crashlytics/_force-test-crash.md +++ b/docs/crashlytics/_force-test-crash.md @@ -6,8 +6,8 @@ If you’ve added an error handler that calls `FirebaseCrashlytics.instance.recordError(error, stack, fatal: true)` to the - top-level `Zone`, you can use the following code to add a button to your app - that, when pressed, throws a test exception: + `PlatformDispatcher.instance.onError`, you can use the following code to add a + button to your app that, when pressed, throws a test exception: ```dart TextButton( diff --git a/packages/firebase_crashlytics/firebase_crashlytics/example/lib/main.dart b/packages/firebase_crashlytics/firebase_crashlytics/example/lib/main.dart index 78516614521a..6aa9084c5663 100644 --- a/packages/firebase_crashlytics/firebase_crashlytics/example/lib/main.dart +++ b/packages/firebase_crashlytics/firebase_crashlytics/example/lib/main.dart @@ -25,13 +25,28 @@ Future main() async { await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); + const fatalError = true; + // Non-async exceptions FlutterError.onError = (errorDetails) { - // If you wish to record a "non-fatal" exception, please use `FirebaseCrashlytics.instance.recordFlutterError` instead - FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails); + if (fatalError) { + // If you want to record a "fatal" exception + FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails); + // ignore: dead_code + } else { + // If you want to record a "non-fatal" exception + FirebaseCrashlytics.instance.recordFlutterError(errorDetails); + } }; + // Async exceptions PlatformDispatcher.instance.onError = (error, stack) { - // If you wish to record a "non-fatal" exception, please remove the "fatal" parameter - FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); + if (fatalError) { + // If you want to record a "fatal" exception + FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); + // ignore: dead_code + } else { + // If you want to record a "non-fatal" exception + FirebaseCrashlytics.instance.recordError(error, stack); + } return true; }; runApp(MyApp());