Firebase is a great backend solution for anyone that wants to use authentication, databases, cloud functions, ads, and countless other features within an app.
In this article, you will create a Firebase project for iOS and Android platforms using Flutter.
To complete this tutorial, you will need:
This tutorial was verified with Flutter v2.0.6, Android SDK v31.0.2, and Android Studio v4.1.
This tutorial will require the creation of an example Flutter app.
Once you have your environment set up for Flutter, you can run the following to create a new application:
- flutter create flutterfirebaseexample
Navigate to the new project directory:
- cd flutterfirebaseexample
Using flutter create
will produce a demo application that will display the number of times a button is clicked.
Now that we’ve got a Flutter project up and running, we can add Firebase.
First, log in with your Google account to manage your Firebase projects. From within the Firebase dashboard, select the Create new project button and give it a name:
Next, we’re given the option to enable Google Analytics. This tutorial will not require Google Analytics, but you can also choose to add it to your project.
If you choose to use Google Analytics, you will need to review and accept the terms and conditions prior to project creation.
After pressing Continue, your project will be created and resources will be provisioned. You will then be directed to the dashboard for the new project.
In order to add Android support to our Flutter application, select the Android logo from the dashboard. This brings us to the following screen:
The most important thing here is to match up the Android package name that you choose here with the one inside of our application.
The structure consists of at least two segments. A common pattern is to use a domain name, a company name, and the application name:
com.example.flutterfirebaseexample
Once you’ve decided on a name, open android/app/build.gradle
in your code editor and update the applicationId
to match the Android package name:
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId 'com.example.flutterfirebaseexample'
...
}
...
You can skip the app nickname and debug signing keys at this stage. Select Register app to continue.
The next step is to add the Firebase configuration file into our Flutter project. This is important as it contains the API keys and other critical information for Firebase to use.
Select Download google-services.json
from this page:
Next, move the google-services.json
file to the android/app
directory within the Flutter project.
We’ll now need to update our Gradle configuration to include the Google Services plugin.
Open android/build.gradle
in your code editor and modify it to include the following:
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.6'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
...
}
}
Finally, update the app level file at android/app/build.gradle
to include the following:
apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:28.0.0')
// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
With this update, we’re essentially applying the Google Services plugin as well as looking at how other Flutter Firebase plugins can be activated such as Analytics.
From here, run your application on an Android device or simulator. If everything has worked correctly, you should get the following message in the dashboard:
Next up, let’s add iOS support!
In order to add Firebase support for iOS, we have to follow a similar set of instructions.
Head back over to the dashboard and select Add app and then iOS icon to be navigated to the setup process.
Once again, we’ll need to add an “iOS Bundle ID”. It is possible to use the “Android package name” for consistency:
You’ll then need to make sure this matches up by opening the iOS project up in Xcode at ios/Runner/Runner.xcodeproj
and changing the Bundle identifier under General:
Click Register app to move to the next screen.
In this step, we’ll need to download the configuration file and add this to our Xcode project.
Download GoogleService-Info.plist
and move this into the root of your Xcode project within Runner
:
Be sure to move this file within Xcode to create the proper file references.
There are additional steps for installing the Firebase SDK and adding initialization code, but they are not necessary for this tutorial.
That’s it!
In this article, you learned how to set up and ready our Flutter applications to be used with Firebase.
Flutter has official support for Firebase with the FlutterFire set of libraries.
In future articles, we’ll look at how to use Firebase features such as Cloud Firestore, Authentication, Analytics, and more with Flutter.
If you’d like to learn more about Flutter, check out our Flutter topic page for exercises and programming projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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. … 2.Register your app with Firebase. … 3. Add a Firebase configuration file. … 4.Add FlutterFire plugins.