On this page12 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Add firebase_crashlytics
  4. 2. The Gradle Plugin (Android)
  5. 3. Off While Developing
  6. Code Examples
  7. Common Beginner Mistakes
  8. Best Practices
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways
  12. Full Code (Copy-Paste Reference)

Lecture 013: Crashlytics Setup

Category4: Crashlytics
Lecture013 of 014
Estimated video length12 min
TargetFlutter 3.44 · Dart 3.12 · FlutterFire (latest)
LevelIntermediate

When your app crashes on a user's phone, you never see the red screen. Crashlytics sends you the error and the exact line. We add the package and the Android Gradle plugin.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Practice Questions · Answers · Takeaways · Full Code


Prerequisites

Lecture 002: the Gradle plugins blocks.


Learning Objectives

  • Add firebase_crashlytics
  • Declare and apply the Crashlytics Gradle plugin
  • Know what the plugin does
  • Switch collection off in debug builds

1. Add firebase_crashlytics

bash
flutter pub add firebase_crashlytics

Then open Firebase console → Run → Crashlytics once and click Enable Crashlytics (or Get started). Until then the dashboard says it's waiting for your app.

2. The Gradle Plugin (Android)

android/settings.gradle.kts:

kotlin
plugins {
    id("dev.flutter.flutter-plugin-loader") version "1.0.0"
    id("com.android.application") version "9.0.1" apply false
    id("org.jetbrains.kotlin.android") version "2.3.20" apply false
    id("com.google.gms.google-services") version "4.5.0" apply false
    id("com.google.firebase.crashlytics") version "3.0.8" apply false   // ← add
}

android/app/build.gradle.kts:

kotlin
plugins {
    id("com.android.application")
    id("dev.flutter.flutter-gradle-plugin")
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")   // ← add
}

The plugin uploads mapping files for release builds. When R8 shrinks and renames your code (a.b.c() instead of PaymentService.charge()), Crashlytics uses them to turn native stack traces back into readable names. Check the Crashlytics docs for the newest plugin version.

iOS needs nothing extra for Dart errors: pod install brings in the library.

3. Off While Developing

Crashes you cause while coding would clutter the dashboard. Collect only in release builds:

dart
import 'package:flutter/foundation.dart';

await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kDebugMode);

kDebugMode is true in flutter run (debug) and false in release builds. While testing Crashlytics itself (Lecture 014), pass true temporarily.


Code Examples

Example 1: main.dart So Far

dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kDebugMode);
  runApp(const MyApp());
}

Run flutter run: the app starts as before. Lecture 014 connects Flutter's errors to Crashlytics.


Common Beginner Mistakes

1. Plugin applied, not declared: "Plugin with id 'com.google.firebase.crashlytics' not found". Add the settings.gradle.kts line.

2. Crashlytics not enabled in the console: reports never appear. Click Enable once.

3. Collection off while testing: with !kDebugMode, debug crashes are never sent. Pass true while you test.


Best Practices

  • Keep collection off in debug builds.
  • Mention crash reporting in your privacy policy.
  • Keep the Crashlytics plugin version up to date with the Google services plugin.

Practice Questions

  1. Which two Gradle files change for Crashlytics?
  2. What does the Crashlytics Gradle plugin upload, and why?
  3. What does setCrashlyticsCollectionEnabled(!kDebugMode) do?
  4. Does iOS need a Gradle-like plugin for Dart errors?

Practice Answers

  1. android/settings.gradle.kts and android/app/build.gradle.kts.
  2. Mapping files, so shrunk release stack traces show real names.
  3. Collects crashes only in release builds.
  4. No.

Key Takeaways

  • flutter pub add firebase_crashlytics and enable Crashlytics in the console.
  • Declare and apply com.google.firebase.crashlytics like the Google services plugin.
  • The plugin keeps release stack traces readable.
  • Turn collection off in debug with !kDebugMode.

Full Code (Copy-Paste Reference)

Everything the app needs up to and including this lecture. firebase_notification_handling.dart, dialog_promo_code.dart, notification_status.dart and analytics_service.dart are unchanged from Lecture 012.

android/settings.gradle.kts (relevant part)

kotlin
plugins {
    id("dev.flutter.flutter-plugin-loader") version "1.0.0"
    id("com.android.application") version "9.0.1" apply false
    id("org.jetbrains.kotlin.android") version "2.3.20" apply false
    id("com.google.gms.google-services") version "4.5.0" apply false
    id("com.google.firebase.crashlytics") version "3.0.8" apply false // ← add
}

android/app/build.gradle.kts (relevant part)

kotlin
plugins {
    id("com.android.application")
    id("dev.flutter.flutter-gradle-plugin")
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics") // ← add
}

lib/main.dart

dart
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'firebase_notification_handling.dart';
import 'firebase_options.dart';

@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  print('Background message: ${message.data}');
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  // kDebugMode is true in `flutter run`, false in a release build - crashes
  // you cause while coding never reach the dashboard.
  await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kDebugMode);

  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static final navigatorKey = GlobalKey<NavigatorState>();
  static final FirebaseAnalytics analytics = FirebaseAnalytics.instance;
  static final FirebaseAnalyticsObserver observer =
      FirebaseAnalyticsObserver(analytics: analytics);

  @override
  void initState() {
    super.initState();
    FirebaseNotificationHandling.registerNotificationPermission();
    FirebaseNotificationHandling.firebaseOnMessageNotificationHandle(navigatorKey);
    FirebaseNotificationHandling.registerWithFcm();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      navigatorObservers: <NavigatorObserver>[observer],
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Notify Demo')),
      body: const Center(child: Text('Allow notifications when prompted')),
    );
  }
}