Google is committed to advancing racial equity for Black communities. See how.

Add Firebase to your Flutter app

Follow this guide to add Firebase products to a Flutter app.

Prerequisites

  • Install your preferred editor or IDE.

  • Make sure that your app meets the following requirements:

    • Targets API level 16 (Jelly Bean) or later
    • Uses Gradle 4.1 or later
  • Set up a device or emulator for running your app. Emulators must use an emulator image with Google Play.

  • Install Flutter for your specific operating system, including the following:

    • Flutter SDK
    • Supporting libraries
    • Platform-specific software and SDKs
  • Sign into Firebase using your Google account.

If you don't already have a Flutter app, you can complete the Get Started: Test Drive to create a new Flutter app using your preferred editor or IDE.

Step 1: Create a Firebase project

Before you can add Firebase to your Flutter app, you need to create a Firebase project to connect to your app. Visit Understand Firebase Projects to learn more about Firebase projects.

Step 2: Register your app with Firebase

  1. In the center of the Firebase console's project overview page, click the Android icon () to launch the setup workflow.

    If you've already added an app to your Firebase project, click Add app to display the platform options.

  2. Enter your app's package name in the Android package name field.

    • A package name is sometimes referred to as an application ID.

    • Find this package name in your module (app-level) Gradle file, usually app/build.gradle (example package name: com.yourcompany.yourproject).

  3. (Optional) Enter other app information as prompted by the setup workflow.

  4. Click Register app.

Step 3: Add a Firebase configuration file

  1. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).

    • You can download your Firebase Android config file again at any time.
    • Make sure the config file is not appended with additional characters, like (2).
  2. Move your config file into the android/app directory of your Flutter app.

  3. To enable Firebase services in your Android app, add the google-services plugin to your Gradle files, as follows:

    1. In your root-level (project-level) Gradle file (android/build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google’s Maven repository, as well.

      buildscript {
      
          repositories {
            // Check that you have the following line (if not, add it):
            google()  // Google's Maven repository
          }
      
          // ...
      
          dependencies {
            // ...
      
            // Add the following line:
            classpath 'com.google.gms:google-services:4.3.4'  // Google Services plugin
          }
      }
      
      allprojects {
          // ...
      
          repositories {
            // Check that you have following line (if not, add it):
            google()  // Google's Maven repository
            // ...
          }
      }
      
    2. In your module (app-level) Gradle file (usually android/app/build.gradle), apply the Google Services Gradle plugin.

      // Add the following line:
      apply plugin: 'com.google.gms.google-services'  // Google Services plugin
      
      android {
        // ...
      }
      
      // ...
      
  4. Run flutter packages get.
    For more information about managing packages and plugins, refer to Using Packages.

  5. Back in the Firebase console setup workflow, click Next to skip the remaining steps.

  6. Continue to Add FlutterFire plugins.

Step 4: Add FlutterFire plugins

Flutter uses plugins to provide access to a wide range of platform-specific services, such as Firebase APIs. Plugins include platform-specific code to access services and APIs on each platform.

Firebase is accessed through a number of different libraries, one for each Firebase product (for example: Realtime Database, Authentication, Analytics, or Storage). Flutter provides a set of Firebase plugins, which are collectively called FlutterFire.

Since Flutter is a multi-platform SDK, each FlutterFire plugin is applicable for both iOS and Android. So, if you add any FlutterFire plugin to your Flutter app, it will be used by both the iOS and Android versions of your Firebase app.

Be sure to check the FlutterFire docs for the most up-to-date list of FlutterFire plugins.

  1. Ensure that your app is not currently running in your emulator or on your device.

  2. From the root directory of your Flutter app, open your pubspec.yaml file.

  3. Add the FlutterFire plugin for the Firebase Core Flutter SDK.

    dependencies:
      flutter:
        sdk: flutter
      # Add the dependency for the Firebase Core Flutter SDK
      firebase_core: ^0.4.0+9
    
  4. Add the FlutterFire plugins for the Firebase products that you want to use in your app.

    Analytics enabled

    dependencies:
      flutter:
        sdk: flutter
      # Check that you have this dependency (added in the previous step)
      firebase_core: ^0.4.0+9
    # Add the dependency for the FlutterFire plugin for Google Analytics firebase_analytics: ^5.0.2
    # Add the dependencies for any other Firebase products you want to use in your app # For example, to use Firebase Authentication and Cloud Firestore firebase_auth: ^0.14.0+5 cloud_firestore: ^0.12.9+5

    Analytics not enabled

    dependencies:
      flutter:
        sdk: flutter
      # Check that you have this dependency (added in the previous step)
      firebase_core: ^0.4.0+9
    # Add the dependencies for the Firebase products you want to use in your app # For example, to use Firebase Authentication and Cloud Firestore firebase_auth: ^0.14.0+5 cloud_firestore: ^0.12.9+5
  5. Run flutter packages get.
    For more information about managing packages and plugins, refer to Using Packages.

  6. If you added Analytics, run your app to send verification to Firebase that you've successfully integrated Firebase. Otherwise, you can skip the verification step.

You’re all set! Your Flutter app is registered and configured to use Firebase.

Try out an example app with Analytics

Like all packages, the firebase_analytics plugin comes with an example program.

  1. Open a Flutter app that you've already configured to use Firebase (see instructions on this page).

  2. Access the lib directory of the app, then delete the existing main.dart file.

  3. From the Google Analytics example program repository, copy-paste the following two files into your app's lib directory:

    • main.dart
    • tabs_page.dart
  4. Run your Flutter app.

  5. Go to your app's Firebase project in the Firebase console, then click Analytics in the left-nav.

    • Click Dashboard. If Analytics is working properly, the dashboard shows an active user in the "Users active in the last 30 minutes" panel (this might take time to populate this panel).

    • Click DebugView. Enable the feature to see all the events generated by the example program.

For more information about setting up Analytics, visit the getting started guides for iOS and for Android.

Next steps