This quickstart describes how to set up Firebase Crashlytics in your app with the Crashlytics Flutter plugin so that you can get comprehensive crash reports in the Firebase console.
Setting up Crashlytics involves using both a command-line tool and your IDE. To finish setup, you'll need to force a test exception to be thrown to send your first crash report to Firebase.
Before you begin
If you haven't already, configure and initialize Firebase in your Flutter project.
Recommended: To get features like crash-free users, breadcrumb logs, and velocity alerts, you need to enable Google Analytics in your Firebase project.
All Android and Apple platforms supported by Crashlytics (except watchOS) can take advantage of these features from Google Analytics.
Make sure that Google Analytics is enabled in your Firebase project: Go to
> Integrations tab, then follow the on-screen instructions for Google Analytics. > Project settings
Step 1: Add Crashlytics to your Flutter project
From the root of your Flutter project, run the following command to install the Crashlytics Flutter plugin:
flutter pub add firebase_crashlytics
From the root directory of your Flutter project, run the following command:
flutterfire configure
Running this command ensures that your Flutter app's Firebase configuration is up-to-date and, for Android, adds the required Crashlytics Gradle plugin to your app.
Once complete, rebuild your Flutter project:
flutter run
(Optional) If your Flutter project uses the
--split-debug-info
flag (and, optionally, the--obfuscate
flag), you need to use the Firebase CLI (v.11.9.0+) to upload Android symbols.From the root directory of your Flutter project, run the following command:
firebase crashlytics:symbols:upload --app=APP_ID PATH/TO/symbols
The
PATH/TO/symbols
directory is the same directory that you pass to the--split-debug-info
flag when building the application.
Step 2: Configure crash handlers
You can automatically catch all errors that are thrown within the Flutter
framework by overriding FlutterError.onError
with
FirebaseCrashlytics.instance.recordFlutterFatalError
:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Pass all uncaught "fatal" errors from the framework to Crashlytics
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
runApp(MyApp());
}
To catch asynchronous errors that aren't handled by the Flutter framework, use
PlatformDispatcher.instance.onError
:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FlutterError.onError = (errorDetails) {
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
};
// Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
runApp(MyApp());
}
For examples of how to handle other types of errors, see Customize crash reports.
Step 3: Force a test crash to finish setup
To finish setting up Crashlytics and see initial data in the Crashlytics dashboard of the Firebase console, you need to force a test exception to be thrown.
Add code to your app that you can use to force a test exception to be thrown.
If you’ve added an error handler that calls
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true)
to the top-levelZone
, you can use the following code to add a button to your app that, when pressed, throws a test exception:TextButton( onPressed: () => throw Exception(), child: const Text("Throw Test Exception"), ),
Build and run your app.
Force the test exception to be thrown in order to send your app's first report:
Open your app from your test device or emulator.
In your app, press the test exception button that you added using the code above.
Go to the Crashlytics dashboard of the Firebase console to see your test crash.
If you've refreshed the console and you're still not seeing the test crash after five minutes, enable debug logging to see if your app is sending crash reports.
And that's it! Crashlytics is now monitoring your app for crashes and, on
Android, non-fatal errors and ANRs. Visit the
Crashlytics dashboard
to view and investigate all your reports and statistics.
Next steps
Customize your crash report setup by adding opt-in reporting, logs, keys, and tracking of additional non-fatal errors.
Integrate with Google Play so that you can filter your Android app's crash reports by Google Play track directly in the Crashlytics dashboard. This allows you to better focus your dashboard on specific builds.
View stack traces and crash statistics alongside your code with the App Quality Insights window in Android Studio (available starting with Electric Eel 2022.1.1).