In this article, we will learn about how to Integrate Firebase in Flutter Applications. A complete mobile application needs features like Authentication, File Storage, Real-time database, Analytics, etc. To include this feature, it requires backend services. Developers need to write their own backend service which costs extra time and effort. So, there is a platform called Firebase which is Backend-as-a-Service (BaaS) that provides all the above-mentioned backend features.
Firebase is a platform developed by Google for creating mobile and web applications. It helps you to build, improve, and grow your app.
Also read: Flutter Flavor: Separating Build Environments In Flutter Apps
Now, let’s begin:
1. Create a Flutter application
flutter create flutter_notifications
2. Create a Firebase Project
We need to first create a new project. Go to Firebase Console and click on Add a Project.
Then fill in the name of the firebase project.
Then follow the next steps and go to console of firebase project.
You need to register your app on both Android and iOS Platforms.
Let’s start with Android App
Click on the android icon and fill in the android package name. You will get the package name of your application in AndroidManiefest.xml file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter_notifications">
Register your app and download the google-service.json file. Add the file into your Android app module root directory as shown in the figure.
Then add Firebase SDK to your project.
Open project-level build.gradle file and add the following code inside the dependencies.
classpath 'com.google.gms:google-services:4.3.10'
Also, open App-level build.gradle (<project>/<app-module>/build.gradle
) and add the following code:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
Lastly, add the following code inside the dependencies.
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'com.google.firebase:firebase-analytics'
Run your app. The firebase is implemented successfully.
(adsbygoogle = window.adsbygoogle || []).push({});Now, Let’s configure in iOS applications.
Now, to configure in iOS Application add the bundle Identifier is available in project.pbxproj file.
Register the iOS Application then download the GoogleService-Info.plist.
Move the GoogleService-Info.plist file that you just downloaded into the root of your Xcode project and add it to all targets.
Then, add the following code in AppDelegate.swift file.
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FirebaseApp.configure()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Now, your app is ready to use all the firebase packages in your Flutter Applications.
Make sure you install the firebase_core plugin in your pubspec.yaml file.
Read more articles about Flutter here.