Setup Firebase
Firebase is Google’s cross-platform app development platform. It provides fully managed backend service infrastructure for web based, android or iOS applications.
Firebase provides extensions for common development tasks like authentication, payments, email, etc — which can be added per project basis.
Create Firebase Project
-
Sign in into your Google account.
-
You need to create a Firebase project, if you don’t already have one. Go to Firebase console here - create a new project. Example:
flutter-starter-kit
. Feel free to disable Google Analytics for this project. Either way, it will not matter - you can enable it later from Project Settings → Integrations.
Required Tools for Firebase - Flutter integration
Firebase CLI
Installing Firebase CLI locally will help you manage, test and deploy Firebase project from your command line. It is required to perform Firebase-Flutter integration as we will be modifying Firebase resources through our runtime environment.
To download and install Firebase CLI, you have two options:
Option 1: Through node package:
Option 2: Through Standalone Binary without any dependency
Verify your installation by running firebase --help
. More help on Firebase tools can be found here: https://firebase.google.com/docs/cli.
Log into Firebase with your Google account using this command:
FlutterFire CLI
FlutterFire CLI is used to create Firebase app for target development environment (Android/iOS/Web/macOS) in your existing Firebase project. It is also possible to create new project through FlutterFire.
Run this command from any directory to install:
This does not add any new file in your project, it is a separate package. Add this to PATH: export PATH="$PATH:$HOME/.pub-cache/bin"
to make sure flutterfire_cli is available in your terminal.
Add Firebase to your Flutter project
This command runs the worflow to add apps to your Firebase project and generate necessary files for your Flutter project to use Firebase:
-
Asks you to select the platforms (iOS, Android, Web) that you want to support in your Flutter app. For each selected platform, the FlutterFire CLI creates a new Firebase app in your Firebase project.
-
Creates a Firebase configuration file (
firebase_options.dart
) and adds it to yourlib/
directory.👋 Note:
firebase_options.dart
file is already included in the project. This command is supposed to override the existing file and add your selected platforms. It will also make changes inios
andandroid
folders. -
Generates a
google-services.json
file for each platform that you selected. -
Adds the necessary dependencies to your
pubspec.yaml
file. -
Adds the necessary code to your
AndroidManifest.xml
file for Android.
After running the command, you should clean and run your project to make sure everything is working.
Congrats! You have successfully added Firebase to your Flutter project!
Common errors
-
Flutter Android: One or more plugins require a higher Android NDK version. Link: here.
Solution: Choose the latest NDK version from here and update in
android/app/build.gradle
. -
Common error when setting up on iOS:
Automatically assigning platform iOS with version 12.0 on target Runner because no platform was specified. Please specify a platform for this target in your Podfile. See https://guides.cocoapods.org/syntax/podfile.html#platform.
OR
Error: The plugin "firebase_auth" requires a higher minimum iOS deployment version than your application is targeting. To build, increase your application's deployment target to at least 13.0 as described at https://docs.flutter.dev/deployment/ios
Solution: Go to ios/Podfile and add
platform :ios, '13.0'
to the top of the file to fix this error. This is already done in the project.