Skip to main content

Flutter Firebase Crashlytics Examples

Flutter Firebase Crashlytics - How To Examples

Introduction

Firebase Crashlytics is a powerful crash reporting tool provided by Firebase that helps developers track and analyze application crashes. It provides detailed crash reports including stack traces, device information, and user metadata to help identify and fix issues quickly.

In this tutorial, we will explore various examples of using firebase_crashlytics package in Flutter to integrate Firebase Crashlytics into your application.

Installation

To get started, you need to add the firebase_crashlytics package to your Flutter project. Open your pubspec.yaml file and add the following dependency:

dependencies:
firebase_crashlytics: ^2.4.1

Save the file and run flutter pub get to install the package.

Example 1: Initializing Firebase Crashlytics

To start using Firebase Crashlytics, you need to initialize it with your Firebase project credentials. Add the following code to your main.dart file:

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

runApp(MyApp());
}

In this example, we initialize Firebase using Firebase.initializeApp() and enable crash reporting by calling setCrashlyticsCollectionEnabled(true). We also set recordFlutterError as the error handler for Flutter errors.

Example 2: Logging Custom Events

You can log custom events to capture additional information about the state of your application. For example, you might want to log when a specific button is clicked or when a certain action is performed. Add the following code to log a custom event:

FirebaseCrashlytics.instance.log('User clicked the login button');

You can view these custom logs in the Firebase Crashlytics console.

Example 3: Logging Non-Fatal Errors

In addition to capturing crashes, you can also log non-fatal errors manually. These could be exceptions or errors that occur during the runtime of your application. Add the following code to log a non-fatal error:

try {
// Code that may throw an exception
} catch (e, stackTrace) {
FirebaseCrashlytics.instance.recordError(e, stackTrace);
}

This will log the error message and the associated stack trace in Firebase Crashlytics.

Example 4: Forcing a Crash

To test the crash reporting functionality, you can force a crash in your application. Add the following code to your application:

FirebaseCrashlytics.instance.crash();

This will immediately cause the application to crash and send a crash report to Firebase Crashlytics.

Example 5: Setting User Identifiers

You can set user identifiers to associate crashes with specific users. This can be useful for tracking crashes in a multi-user application. Add the following code to set a user identifier:

FirebaseCrashlytics.instance.setUserIdentifier('user123');

You can view crash reports associated with a specific user in the Firebase Crashlytics console.

Example 6: Setting User Metadata

In addition to user identifiers, you can also set custom metadata to provide more context about a crash. This could include user preferences, app settings, or any other relevant information. Add the following code to set user metadata:

FirebaseCrashlytics.instance.setCustomKey('preference', 'dark mode');

You can view this metadata in the Firebase Crashlytics console to help diagnose and fix crashes.

Example 7: Logging Network Requests

You can log network requests made by your application to help identify issues related to network connectivity or server responses. Add the following code to log a network request:

FirebaseCrashlytics.instance.log('GET /api/user');

You can view these network logs in the Firebase Crashlytics console.

Example 8: Disabling Crash Reporting

If you want to temporarily disable crash reporting, you can do so by calling setCrashlyticsCollectionEnabled(false). Add the following code to disable crash reporting:

FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false);

No crash reports will be sent to Firebase Crashlytics while crash reporting is disabled.

Example 9: Catching Flutter Errors

By default, Flutter errors are automatically reported to Firebase Crashlytics. However, you can also catch and log Flutter errors manually if needed. Add the following code to catch and log Flutter errors:

FlutterError.onError = (FlutterErrorDetails details) {
FirebaseCrashlytics.instance.recordFlutterError(details);
};

This will override the default error handler and use Firebase Crashlytics to log Flutter errors.

Example 10: Customizing Crash Reports

Firebase Crashlytics provides options to customize the crash reports sent to the console. You can add custom keys, log breadcrumbs, and enable/disable automatic crash reports. Refer to the firebase_crashlytics documentation for more information on customization options.

These were just a few examples of how to use firebase_crashlytics package in Flutter. With Firebase Crashlytics, you can easily track and diagnose crashes in your application, enabling you to provide a more stable and reliable experience for your users.

Remember to import the necessary packages and follow the best practices recommended by Firebase to get the most out of Firebase Crashlytics in your Flutter application.