Mobile applications need backend services like user authentication, file storage, real-time database, analytics etc. Firebase is a Backend-as-a-Service (BaaS) app development platform which provides hosted backend services. In order to use firebase services in a flutter application, we first need to integrate firebase with our flutter application.
How to integrate firebase in flutter application?
These are the few steps to integrate firebase in flutter application.
Open the firebase console click on add project to add it in flutter application. Firstly, give the project name, and then click continue, that would create the firebase project. Next, you will be given the option to enable google analytics that you can 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 before the project creation.
After pressing continue, the project will be created and resources will be provisioned. Then it will be redirected to the dashboard for the new project.
- Add your flutter application in firebase project
As flutter supports both iOS and android, you need to add firebase in modules for both android and iOS, if you want to run your application on both platform. For adding firebase to android module click on the android icon.
You will need to give below data in the form.
To get the package name for the firebase, open android/app/build.gradle in your code editor and update the applicationId to match the android package name:
Application Nick Name
You can give some Application nick name; it can be anything.
In the android module, open the gradle panel from right top side and expand the android, which is the base option, then expand app, then task, then android. Now the SHA-1 Key can be generated by clicking on signingReport option.
After giving all the required data, click on next and download the google-services.json file. Now open the android module, and in the project panel, switch from android mode to Project, then inside the root directory, in the app folder, paste the google-services.json file.
Now you need to add some code in (app) build.gradle, (project) Android/build.gradle. Open the project build.gradle and add the below line inside the dependencies.
Now open the app-level build.gradle file and add firebase sdk for different firebase services. For common services like firebase auth, firestore, and analytics add below dependencies in app build.gradle file.
//For Firebase Analytics Service
//For Firebase Authentication Service
//For Firebase Firestore Database Service
Sometimes, after adding firebase in android module, it gives multidex error while building the project. So, it is better to enable multidex to avoid any such build error. In the app gradle.build file, add the below dependency in dependencies.
At last, in the app gradle.build, inside the defaultConfig, add this line below minSdkVersion
Configuring an iOS app
In order to configure firebase in ios app
From the project dashboard, click the Add app button
- Click the iOS icon
- Enter the bundle ID and click Register app in the Register section
- Open the Xcode, download the GoogleService-Info.plist file, and drag and drop into the Runner subfolder
- Follow the instructions as described in the “Add Firebase SDK” and “Add initialization code” sections
- Click on Continue to console to complete the setup step.
Open flutter pubspec.yaml file in order to add dependencies to use firebase plugins. Paste the below lines in the dependencies
Finally, to set up the firebase for the flutter project, go to the main method and make the following changes
Now you can build your flutter application and use the firebase services in your flutter application.