Add Firebase to your Flutter app

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

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 above.
    • Android development — The app must target API level 14 (Ice Cream Sandwich) or above.
  • Access 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.

Create a Firebase project

Before you can add Firebase to your Flutter app, create a Firebase project to connect to your Flutter app.

To create a Firebase project:

  1. Go to the Firebase console.

  2. Click Add project, then select or enter a Project name.

    • If you have an existing Google project associated with your app, select the project from the Project name dropdown menu.
    • If you don't have an existing Google project, enter a new Project name.
  3. (Optional) Edit the Project ID.

    Firebase automatically assigns a unique ID to your Firebase project. This identifier displays in publicly visible Firebase services, for example:

    • Default database URL — your-project-id.firebaseio.com
    • Default hosting subdomain — your-project-id.firebaseapp.com
  4. Follow the remaining setup steps, then click Create project (or Add Firebase, if you're using an existing Google project).

Firebase automatically provisions resources for your Firebase project. The process typically takes a few minutes. When the process completes, you'll be taken to the overview page for your Firebase project in the Firebase console.

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, see 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 your Firebase project overview page, launch the setup wizard for iOS.

  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 wizard.

    3. Click Register app.

  3. Obtain the Firebase configuration file:

    1. Click Download GoogleService-Info.plist.

    2. Move the file into the Runner/Runner directory of your Flutter app.

  4. Back in the Firebase console, in the iOS setup wizard dialog, click Next to skip the remaining steps.

  5. Continue to Add FlutterFire plugins.

Configure an Android app

  1. In your Firebase project overview page, launch the setup wizard for Android.

  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 wizard.

    3. Click Register app.

  3. Obtain the Firebase configuration file:

    1. Click Download google-services.json.

    2. Move the 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 {
        // ...
        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' // Gradle plugin
      
    3. Run flutter packages get.

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

  5. Continue to Add FlutterFire plugins.

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 service (for example: database, authentication, analytics, or storage). Flutter provides a set of Firebase plugins, which is 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 SDK.

    dependencies:
      flutter:
        sdk: flutter
      firebase_core: ^0.2.5  // add dependency for Firebase Core
    
  4. Add additional FlutterFire plugins to use specific Firebase services.

    The following section, Add Analytics, is an example of how to add FlutterFire plugins to your app.

  5. Run your app to verify installation.

Your Flutter app should now be connected to Firebase.

Add analytics (optional)

Adding Google Analytics for Firebase to your Flutter app is optional, but it's a relatively easy way to verify that Firebase is working properly with Flutter. In addition, Analytics can be very useful during development and debugging, as well as helping to track the success of your production apps.

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

  2. In your pubspec.yaml file, add the FlutterFire plugin for Google Analytics for Firebase:

    dependencies:
      flutter:
        sdk: flutter
      firebase_core: ^0.2.5       // This dependency was added during initial configuration.
      firebase_analytics: ^1.0.4  // add dependency for Google Analytics for Firebase
    
  3. Run flutter packages get.

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

Analytics app example

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 for Firebase 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, see the getting started guides for iOS and for Android.

More about Firebase and Flutter

Оставить отзыв о...

Текущей странице
Нужна помощь? Обратитесь в службу поддержки.