Add Firebase Crashlytics to Flutter


(This article is a work in progress. Keeping it here for now as reference)

This was written when Flutter was at version 3.27. But will probably work fine with little adjustments in later versions. I’m focusing on Android, but many steps apply for iOS too. I will update the article in the future to cover more platforms.

1. Set Up Firebase Project

  1. Go to the Firebase Console
  2. Create a new project or select an existing one.
  1. Add your Flutter app:

Follow the instructions on Firebase, and download the google-services.json file. Put it in the android/app folder.

2. Add Firebase SDK to Your App

  1. Add the required firebase_crashlytics and firebase_core dependencies to your pubspec.yaml
dependencies:
  firebase_core: ^3.9.0
  firebase_crashlytics: ^4.2.0
YAML
  1. And run
dart pub get
Bash
  1. Add the google services and firebase plugins to Android.

For this you can’t really follow the instructions on Firebase. The documentation for doing Android level stuff in Flutter is sometimes a bit off (even in Flutters own docs).

So if you’re following the guide on Firebase, you’ll probably be looking for the plugins section in your build.gradle. It’s not there… It’s in settings.gradle

Add google services plugin to android/settings.gradle

// android/settings.gradle
plugins {
    ...
    id 'com.google.gms.google-services' version '4.4.2' apply false
    id "com.google.firebase.crashlytics" version "2.8.1" apply false
}
Kotlin

Add plugins to android/app/build.gradle

// android/app/build.gradle
plugins {
    ...
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}
Kotlin
  1. Install Firebase CLI and FlutterFire
  2. Use firebase cli to configure:
flutterfire configure
Bash

3. Test Crashlytics

  1. Enable Crashlytics in Flutter
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize Firebase
  await Firebase.initializeApp();

  // Enable Crashlytics
  FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

  runApp(MyApp());
}
Dart
  1. Throw a test error somewhere in your code. Maybe on a button or gesture detector event. With this simple line:
onTap: () {
  FirebaseCrashlytics.instance.crash();
},
Dart
  1. Go to the Firebase console and check for Crashlytics. If it doesn’t show up directly in the left menu, you can go to All Products and scroll down. Under Run almost at the bottom you’ll find Crashlytics.

Leave a Reply

Your email address will not be published. Required fields are marked *