How to Integrate Firebase Crashlytics in your Flutter App
Tags: firebase, flutter, mobile, beginners
Published: 2023-01-10 14:42:07

Firebase Crashlytics is a lightweight, real-time crash reporter that collects, organize and group crash report highlighting the circumstances that led to them. It provides you with a platform to track, monitor, and prioritize these unexpected crash issues that arise in your mobile apps.  For more reasons why you need Firebase Crashlytics, visit this [article post](https://dev.to/calebj/why-you-need-firebase-crashlytics-in-your-flutter-apps-3lk) that explains in detail.

In this article, we’ll be looking at how to integrate Firebase Crashlytics into a sample flutter app. This is a Firebase login authentication app that makes use of the provider state management architecture. To follow up with the integration of Firebase Crashlytics, check out the starter project on [Github](https://github.com/calebjesusegun/firebase_authentication_app/tree/starter_project).

Specifically, we’ll cover:

- Setting up Crashlytics on the Firebase Console
- Integrating Crashlytics in a Flutter app
- Platform Integration for IOS
- Configure Crash Handlers
- Testing our sample app

Shall we?

### Integrating Crashlytics on Firebase Console

First of all, Open the Firebase console to get started with Crashlytics. Then go to the **Release & Monitor** section in the side panel of the console and click on ‘**Crashlytics**’. Next, you’ll need to select the application either Android or IOS that you’re building for.


![Firebase Crashlytics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1pzripbvqonw205f7u6.png)

When this is done, click on the **Enable Crashlytics** button to set up Crashlytics. After it’s been setup, you’ll be navigated to the Crashlytics Dashboard


![Firebase Crashlytics Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zd3qixy1g9byoceyajj.png)


Integrating Crashlytics in a Flutter app

We have set up Crashlytics in our Firebase console, the next step is to integrate it into our authentication app. Check out the starter project on [Github](https://github.com/calebjesusegun/firebase_authentication_app/tree/starter_project).

The first step is to add the Firebase dependency that is responsible for connecting your Flutter app to your firebase project.


![firebase core](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/664ecnskb1wl7mebev9y.png)

Next, within the main top-level function in our flutter app ensure `WidgetsFlutterBinding` is initialized and then initialize Firebase

![WidgetsFlutterBinding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6q6yoqy5ddma84vf1w9.png)

Then we add the firebase_crashlytics dependency in pubspec.yaml file


![firebase_crashlytics dependency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82glyfy0fmu08marf2ac.png)

Next, we add the dependency to the android/app/build.gradle file path using the command below


![dependency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjuvrn9n9rmbdug8q91i.png)

Then lastly we need to add the classpath in the android/build.gradle file

![classpath](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehwpwssxcolqe73w64zi.png)


### Platform Integration for IOS

To generate crash reports for IOS apps, Firebase Crashlytics needs to access your project's debug symbol (dSYM) files. You can configure Xcode to automatically produce your dSYMs, process them, and upload the files whenever you build your app through the following steps:

1. Open your project's Xcode workspace and select its project file in the left navigation.
2. From the TARGETS list, select your main build target.
3. Then click on the Build Settings tab, click All and search for debug information format.
4. Set the Debug Information Format to DWARF with dSYM File for all your build types. This allows Xcode to produce dSYMs for your builds.
5. Next is to process your dSYMs and upload the files.
6. Click the Build Phases tab and select + > New Run Script Phase. 
7. Make sure to double-check that the new run script phase is your project's last build phase otherwise Crashlytics won't properly process dSYMs.
8. Then expand the new Run Script section and add the following script under the Shell label

![Run Script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e167pfletkst75zttpvv.png)
 
Then lastly in the input files section, add the paths for the locations of the following files:

- 
The location of your project's dSYM files:

![project's dSYM files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr803z0dfd98qgy8r43a.png)

- 
Then the location of your project's built Info.plist file:


![Info.plist](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckvznnmecxvno2pu5b6f.png)

For more information on dSYM files and how to manually upload dSYM files, visit [Get Deobfuscated Reports](https://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?platform=ios)


### Configure Crash Handlers

Crashlytics is now enabled in our authentication app. You can automatically catch all errors that are thrown within the Flutter framework by overriding `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterFatalError` in the main top-level function of the app.


![Uncaught Errors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/531kg0atkwmbftg7ozjy.png)

When it comes to asynchronous errors that are not handled by the Flutter framework, you use `PlatformDispatcher.instance.onError`:


![Asynchronous Errors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8r0tr3ibzo8jp8zu65i.png)

Sometimes not all errors are caught by Flutter, there are zoned errors that can also occur as well. A common use-case where a Flutter error would not be enough is when an exception happens inside of an onPressed button. To catch such errors, you can use `runZonedGuarded`:


![Uncaught errors in Flutter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cybp5z0obp1ahndo7yoo.png)

### Testing our sample app

In order to check whether the Firebase Crashlytics is active or not in the app, Go to the login_view and comment out the function that authenticates a user that wants to log in. Add this line of code in order to force a crash in our app.


![Testing the sample app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu4m29o6pzslc7xqosjy.png)

It will make the app crash forcefully. Then you can go to the Crashlytics Dashboard to check the status report of the crash.


![Firebase Crashlytics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55bbq85hkrwnk2ba44fi.png)


### Conclusion

We’ve successfully integrated Firebase Crashlytics into our sample authentication app. In this article, you were introduced to Firebase Crashlytics, how to easily integrate it into your mobile apps and how to test for unexpected crashes when they occur.

Check out the complete [source code](https://github.com/calebjesusegun/firebase_authentication_app) for the sample app. If you have any questions or inquiries, feel free to reach out to me on Twitter: [@CalebJ](https://twitter.com/CalebJesusegun) or LinkedIn: [@CalebJ](https://www.linkedin.com/in/caleb-jesusegun/).