Flutter: Record Fatal Errors using Firebase Crashlytics (How to)
4 min readDec 25, 2022
Flutter How To: Simple| To the point| Short
Flutter Dev Level: Intermediate
Objective
To record Fatal errors using Firebase crashlytics.
Github
- Find the sample project here: https://github.com/AbdurM/flutter_firebase_crashlytics
Step -1: Add Packages
- 1.1- While being in the root folder of the project, run the following command in the terminal
flutter pub add firebase_core
- 1.2- once the above is successfully installed, run
flutter pub add firebase_crashlytics
Step -2: Create Firebase Project on firebase console (Skip to Step 3 if you already have one)
- 2.1 Go to console.firebase.google.com. Sign in or Sign up (Its free)
- 2.2 Create new project and follow through the easy steps.
Step- 3: Configure firebase in your flutter project
- 3.1- Get back to your code editor (VS Code in my case) or open root of your project inside the terminal.
- 3.2 — While staying in the root of your project, run the following command
flutterfire configure
- 3.3 — You’ll get a list of projects from your firebase console, select the project that you just created(Step 2) or want to use
- 3.4- Select the platforms
- 3.5- Once done and the process successfully completes, you’ll see the following files in your project.
Step 4- Adding the crash handlers
- 4.1- Make the main method of the app async by adding the async keyword
- 4.2- Add the following two lines of code before run app in the main method.
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
- 4.3- Override flutter on error to record Fatal errors after the above two lines but before run app
FlutterError.onError = (flutterErrorDetails) =>
FirebaseCrashlytics.instance.recordFlutterFatalError(flutterErrorDetails);
- 4.4- To record async exception (which btw are not handled by the flutter framework, add the following lines of code in the main method before run app.
PlatformDispatcher.instance.onError = ((exception, stackTrace) {
FirebaseCrashlytics.instance.recordError(exception, stackTrace);
return true;
});
- 4.5- This is how my main method looks at the end
Step 5 — Test Crash the app.
- 5.1- You can crash the app by calling the following api from FirebaseCrashlytics
FirebaseCrashlytics.instance.crash();
- 5.2- To test, I am calling it from a button press
- 5.3- Click the crash the app button (Or whatever code you wrote, call it) and the app will crash.
Step -6 Check the firebase console for crash report
- 6.1- On your firebase console project (console.firebase.google.com), click on Crashlytics under Release & Monitor section of the left pane.
- 6.2- Select the platform where you crashed the app (In my case iOS) from the dropdown
- 6.3- You’ll see the following screen and crash recorded at the bottom (note: Since I was running on the emulator in debug mode, I had to restart the app for this error to appear here).
- 6.4- You can click on it to get more details
Github Link: https://github.com/AbdurM/flutter_authentication
That’s all for this one. Hope it helped :-)
Please find more flutter articles here: https://medium.com/@rafayk66/abdurs-flutter-articles-c201eff334b8
You can find me on
LinkedIn — https://www.linkedin.com/in/knowabdur
Twitter: — https://twitter.com/AbdurDeveloper
#flutter #flutterdeveloper #AbdurDeveloper #fluttercommunity