How to add Firebase to Flutter

Key takeaways:

  • Firebase provides real-time databases, authentication, cloud storage, and analytics to build, improve, and grow Flutter applications efficiently.

  • The FlutterFire CLI simplifies Firebase integration by automating project configuration and platform-specific setup.

  • Firebase ensures seamless integration across platforms like Android, iOS, and web using the DefaultFirebaseOptions.currentPlatform setup.

  • Tools like performance monitoring, crash reporting, and analytics empower developers to optimize user experience and address issues proactively.

Firebase is a backend platform that offers a wide range of services for building mobile applications. It provides developers with tools and infrastructure to build, improve, and grow their apps. Integrating Firebase with Flutter gives a bunch of benefits to the developers.

The major ones are mentioned below.

  • Real-time database: It enables real-time data synchronization, allowing our app to instantly reflect changes made by any user.

  • Authentication: It provides ready-to-use authentication methods, such as email/password, social media login (e.g., Google, Facebook, Twitter), and more.

  • Cloud firestore: It is a flexible, scalable NoSQL document database. It allows us to store, retrieve, and query data easily on a larger scale.

  • Cloud storage: It provides secure and scalable cloud storage for our app's files, including images, videos, and documents.

  • Performance monitoring: It helps us monitor our app's performance, such as network latency, rendering time, and UI freezes.

  • Analytics and crash reporting: It also monitors user behavior and engagement, allowing us to make data-driven decisions. Firebase Crashlytics helps us to track and analyze app crashes, enabling us to fix issues proactively.

Prerequisites

Before adding Firebase to our Flutter project, have the following prerequisites.

  • Make sure we have Flutter SDK installed on your machine.

  • We have logged into the Firebase console.

  • Installed Firebase and FlutterFire CLI.

Note: For the detailed installation of FlutterFire CLI, check out this Answer.

  • Set up a new Flutter project or open an existing one in our preferred IDE.

Adding Firebase to Flutter project

We can add Firebase to the Flutter project by following the instructions below.

Configure Firebase project

We will use the FlutterFire CLI to configure our Flutter applications.

Navigate to the project root directory and run the following command to start the configuration process.

flutterfire configure

After running the above command, it will list all the existing projects from the Firebase console. We can navigate between them using the up and down arrow keys. We can choose a project from the existing one or create a new one by pressing the enter key.

After choosing create a new project, we will have to write down a project ID for our new Firebase project.

Project ID should be unique among all other Firebase projects. Otherwise, it will give an error. A valid project ID could be 'example-app-12345'.

After writing the project ID, we will have to choose the platforms on which this application will run. Select or deselect using the space key and press "Enter" key to continue.

After the platform configurations, it will ask for permission to change the android/build.gradle and android/app/build.gradle files to apply configuration so we don't have to do it manually.

After that, firebase_options.dart will be automatically generated in lib folder. This folder will contain all the information related to the Firebase services, which we will avail, and the project IDs.

Initialize Firebase

After the configuration is completed, we can import the core plugin of Firebase by running the following command in the terminal.

flutter pub add firebase_core

Now we can import the generated firebase_options.dart file and firebase_core package into the dart file. Here, the main() function is defined by writing the following lines of code at the top of the file.

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

Also, initialize Firebase by adding the following code in the main() function.

await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

The DefaultFirebaseOptions.currentPlatform ensures that the appropriate configuration options are used depending on whether the code runs on Android, iOS, or the web. It simplifies the initialization process by automatically selecting the correct options for the current platform.

After completing all the above steps, we can rebuild our Flutter application using the following:

flutter run

Conclusion

Integrating Firebase with Flutter offers numerous benefits, including real-time database synchronization, authentication, cloud storage, serverless functions, performance monitoring, analytics, and crash reporting.

Adding Firebase to your Flutter project can be done using either the Firebase CLI or the manual configuration method. The Firebase CLI provides a convenient way to set up Firebase in your project using the command-line interface with just a few commands.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do I add Firebase notifications to Flutter?

To add Firebase notifications to a Flutter application, you need to integrate Firebase Cloud Messaging (FCM) into your project. Start by setting up a Firebase project in the Firebase console and enabling Firebase Cloud Messaging. Configure your app by adding the required configuration files (e.g., google-services.json for Android and GoogleService-Info.plist for iOS) to your Flutter project. Use the FlutterFire CLI to automate the setup if needed. Install the firebase_messaging package in your Flutter project, and configure it to handle push notifications by initializing Firebase in the main() function. Customize notification behavior by implementing foreground and background message handlers. Finally, test notifications by sending them from the Firebase console or your backend server.


How do I add Firebase performance to Flutter?

To add Firebase Performance Monitoring to a Flutter application, begin by setting up a Firebase project in the Firebase console and enabling the Performance Monitoring feature. Add the necessary Firebase configuration files (google-services.json for Android and GoogleService-Info.plist for iOS) to your Flutter project. Install the firebase_performance package, and ensure Firebase is initialized in your main() function. The performance monitoring tool automatically tracks key metrics such as app start time, network requests, and rendering performance. You can also add custom traces to measure specific app events or operations. Once integrated, performance data will be available in the Firebase console for analysis, helping you optimize your app’s performance.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved