Firebase Crashlytics in Flutter

Piumi Maheshika
3 min readJan 7, 2021

--

Firebase Crashlytics is a great tool for developers to get an idea of what went wrong in their app. More over Firebase Non fatal error reporting feature allows reporting non fatal exceptions into Firebase console making it easier to troubleshoot.

In this article Let’s talk about how to integrate Firebase Crashlytics into Flutter - iOS in 4 steps assuming you already have a Firebase project created and have an idea on how to use Firebase console.

Let’s start👇🏼

  1. Add dependency “firebase_crashlytics” into pubspec.yaml (Read more on firebase_crashlytics here)
firebase_crashlytics: ^0.2.4

2. Inside your Firebase project, register a new iOS app specifying Bundle ID and download the GoogleService_Info.plist file.

Add the plist file into your flutter project > iOS > Runner

3. Add below code into main.dart file

Make sure to call WidgetsFlutterBinding.ensureInitialized() before calling runApp.

Inside the Wrapper widget, I initialise Firebase using a FutureBuilder to make sure Firebase is initialised before launching anything else. in my case; the DemoApp.

Ok this is sufficient to report any app crashes into Firebase console.

But if you need to report any logs or non fatal (caught exceptions) to firebase, this is how to do it.

Feel free to rename the custom keys as you wish.

This is how a reported non fatal error looks on firebase console.

This will help us to have a clear insight on what is happening in our app in non debug environment.

4. Add a new run script into Xcode Build Phases and paste the below code.

# Type a script or drag a script file from your workspace to insert its path.${PODS_ROOT}/FirebaseCrashlytics/run

Run the app and force a crash and relaunch the app for crash report to appear on Firebase console.

Use below code to force a crash on your app.

FirebaseCrashlytics.instance.crash();

5. [Optional] Integrating Firebase in multiple build schemes.

If you have one build scheme then Steps 1– 4 is enough to make it a go. But if you are dealing with multiple build schemes and one target (I did not find any easy way to implement multiple targets in Flutter iOS), we have to provide correct GoogleService_Info.plist at the run time.

In my case I have 3 build schemes for SIT, UAT and Production hence three plist files.

In Xcode let’s add a new group under Runner named Config. Add all the three GoogleService_Info.plist files inside this folder. When adding new files make sure to UNCHECK copy items if needed option. This will add the files at the compile time but what we try to accomplish here is to add files at run time.

Add a new run script and rename it to “Setup Firebase plist files” and copy the code below. Replace the app identifier as relevant.

if [ “${PRODUCT_BUNDLE_IDENTIFIER}” == “<Identifier_SIT>” ]; thencp -r “${PROJECT_DIR}/${PRODUCT_NAME}/Config/GoogleService-Info-SIT.plist” “${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist”echo “SIT”elif [ “${PRODUCT_BUNDLE_IDENTIFIER}” == “<Identifier_UAT>” ]; thencp -r “${PROJECT_DIR}/${PRODUCT_NAME}/Config/GoogleService-Info-UAT.plist” “${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist”echo “UAT”elsecp -r “${PROJECT_DIR}/${PRODUCT_NAME}/Config/GoogleService-Info-PROD.plist” “${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist”echo “PRODUCTION”fi

This command will copy the correct plist file to destination directory with the name of “GoogleService-Info.plist” as required by Firebase.

To see if it successfully copies the correct plist, build the app. Expand the Products. Right click on Runner.app and select “show in finder”.

Inside the finder window, look for Runner and right click and select “show package contents”.

Hope you find this useful 👍 Happy coding 😺

--

--

No responses yet