How To

Setup Firebase in Flutter (Visual Guide)

Last updated: 2020-07-25

This guide was tested on Flutter 1.17.5.

Flutter 1.17.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8af6b2f038 (4 weeks ago) • 2020-06-30 12:53:55 -0700
Engine • revision ee76268252
Tools • Dart 2.8.4

Firebase Account Setup

Create Firebase Project

Firebase Project

Set Project's Name

Firebase Project

Configure Firebase Analytics

Firebase Project

Firebase Project

Firebase Project

Select Your Target: iOS or Android

Firebase Project

Register App

Firebase Project

Download Firebase Config File

Firebase Project

Add Firebase SDK

Firebase Project

In android/build.gradle (root-level / project-level)

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.3'  // Google Services plugin
    }
}

allprojects {
    // ...

    repositories {
      // Check that you have following line (if not, add it):
      google()  // Google's Maven repository
      // ...
    }
}

In android/app/build.gradle (app-level)

// Add the following line:
apply plugin: 'com.google.gms.google-services'  // Google Services plugin

android {
  // ...
}

// ...

Finalize Firebase Setup

Firebase Project

Setup Firestore Database

Firebase Project

Create Database

Firebase Project

Set Database Location

Firebase Project

Firebase Project

Add Entries to Database

Firebase Project

Firebase Project

Firebase Project

Firebase Project

Configure Your Flutter App

Add FlutterFire plugin to pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  firebase_core:
  cloud_firestore:

Run flutter packages get.

Troubleshootings

Error Plugin project :firebase_core_web not found

Add the following content to android/app/settings.gradle:

def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
    pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}

plugins.each { name, path ->
    def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
    include ":$name"
    project(":$name").projectDir = pluginDirectory
}

(Reference)

Error D8: Cannot fit requested classes in a single dex file

Enable multidex support by adjusting android/app/bundle.gradle:

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

By the way, check my free Flutter course on YouTube