Naar console

Add Firebase to your Flutter app

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

Step 1: Set up your environment

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

    • Flutter SDK
    • Supporting libraries
    • Platform-specific software and SDKs
  • Install your preferred editor or IDE, such as Android Studio, IntelliJ, Xcode, or VS Code.

  • Open your Flutter app in your preferred editor or IDE.

    • iOS development — The app must target iOS 8 or later.
    • Android development — The app must target API level 16 (Jelly Bean) or later.
  • Set up a device or emulator for running your app.

    • Android development — Emulators must use an emulator image with Google Play.
  • Sign into Firebase using your Google account.

Step 2: 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 3: Configure your app to use Firebase

You can configure one or more apps to connect with your Firebase project. To do this, register your app's platform-specific ID with your Firebase project. Firebase will automatically generate configuration files for your app.

For detailed instructions to configure your app, follow the platform-specific steps below for iOS and for Android.

In the top-level directory of your Flutter app, the ios and android directories hold platform-specific configuration files for iOS and Android.

Configure an iOS app

  1. In the center of the Firebase console's project overview page, click the iOS 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. Add your app to your Firebase project:

    1. Enter your app’s bundle ID in the iOS bundle ID field.

      • Find this bundle ID by opening your app in XCode, then accessing the General tab in the top-level Runner directory. The value of the Bundle Identifier field is the iOS bundle ID (for example, com.yourcompany.yourproject).
    2. (Optional) Enter other app information as prompted by the setup workflow.

    3. Click Register app.

  3. Add the Firebase iOS configuration file to your app:

    1. Click Download GoogleService-Info.plist to obtain your Firebase iOS config file (GoogleService-Info.plist).

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

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

  5. Continue to Add FlutterFire plugins.

Configure an Android app

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

  2. Add your app to your Firebase project:

    1. Enter your app’s application ID in the Android package name field.

      • An application ID is sometimes referred to as as a package name.

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

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

    3. Click Register app.

  3. Add the Firebase Android configuration file to your app:

    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.

  4. To enable Firebase services in your Android app, add the google-services plugin to your Gradle files.

    1. In your root-level (project-level) Gradle file (android/build.gradle), add rules to include the Google Services 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:3.2.1'  // 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), add the following line to the bottom of the file.

      dependencies {
        // ...
      }
      
      // ...
      
      // Add the following line to the bottom of the file:
      apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin
      
    3. Run flutter packages get.

      For more information about managing packages and plugins, refer to Using Packages.

  5. 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+1
    
  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+1
    # Add the dependency for the FlutterFire plugin for Google Analytics firebase_analytics: ^4.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.11.1+3 firebase_firestore: ^0.12.7+1

    Analytics not enabled

    dependencies:
      flutter:
        sdk: flutter
      # Check that you have this dependency (added in the previous step)
      firebase_core: ^0.4.0+1
    # 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.11.1+3 firebase_firestore: ^0.12.7+1
  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.

  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