(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
- Go to the Firebase Console
- Create a new project or select an existing one.
- 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
- Add the required
firebase_crashlytics
andfirebase_core
dependencies to yourpubspec.yaml
dependencies:
firebase_core: ^3.9.0
firebase_crashlytics: ^4.2.0
YAML- And run
dart pub get
Bash- 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
}
KotlinAdd plugins to android/app/build.gradle
// android/app/build.gradle
plugins {
...
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
}
Kotlin- Install Firebase CLI and FlutterFire
- Use firebase cli to configure:
flutterfire configure
Bash3. Test Crashlytics
- Enable Crashlytics in Flutter
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Firebase
await Firebase.initializeApp();
// Enable Crashlytics
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
runApp(MyApp());
}
Dart- 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- 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