Flutter Firebase

What is Firebase?

Firebase is a platform made by Google. It is used for developing mobile and web development. It was founded in 2010 by James Tamplin and Andrew Lee.

Firebase is a backend as a service (Baas), which means it gives developers access to backend services such as authentication, storage, database, and hosting.

Let's now look closely and see why Firebase is so important?

  1. AdMob: It helps to monetize our application, which means it gives us a way of earning money by showing ads with the application content.
  2. Machine learning kit: It helps in making our application more personalized and engaging. It also provides APIs that we can use in our application.
  3. Hosting: It is used for deploying static websites and single-page applications. Firebase Hosting is very efficient and free of cost as well.
  4. Real-time Database: The database is a NoSQL database on the cloud. We can easily store and synchronize data in real-time.
  5. Authentication: It provides backend services and SDKs. It even facilitates the authentication part of an application. It also improves UX and gives access to large libraries of UI themes.
  6. Firebase Cloud Messaging: It gives us access to contact applications and servers via messaging.
  7. Cloud functions: It is a serverless framework which gives us complete access to develop scripts, user authentication, and more.
  8. Performance Monitoring: As the name itself suggests, it monitors our application's performance by collecting information or data from the user's perspective.
  9. Storage: Firebase gives access to a storage facility; we can store images, audio, videos, and more. This is a cost-efficient and effective storage service.

Setting Up Firebase for Flutter

STEP 1: Go to https://console.firebase.google.com/u/0/?pli=1, and we will see some screen like this. We will click on Create a Project.

Flutter Firebase

STEP 2: When we click on Create a Project, we will be directed to a new screen. There we will have to give our project name and accept the terms and conditions. And in return, they will give us a unique identifier for our project.

Flutter Firebase

STEP 3: After we have successfully completed all the steps involved, our firebase project will be done in a few seconds.

Flutter Firebase
Flutter Firebase

STEP 4: When we click continue, we will be directed to a new page, which will give us access to all the features that are included in Firebase.

Flutter Firebase

STEP 5: In this step, we will now connect our flutter application to Firebase. There are separate ways of doing that in iOS and Android; we will see each one.

Firstly, we will configure an android application.

Go to Project Settings.

Flutter Firebase

Currently, we have no applications for our project. So, we will click on the android icon. From there, we will be redirected to a new page. From there, we will register our android app application.

Flutter Firebase

While registering, we will have to add our android package name; everything else is optional.

Flutter Firebase

NOTE: We can find your Android app's package name in the android/app/build.gradle file.

After the registration is completed, we will get a .json file, and that file is added to our application project. In Android, we will find this file in the android/app directory.

The last step involved in this is to add Firebase SDK to the Gradle files. First and foremost, we will add the google-services dependency to classpath in the android/build.gradle file like this:

buildscript {    repositories {               google() }
dependencies { classpath 'com.google.gms:google-services:3.2.1'  }  }  

After this, we will activate the plugin by typing some code in android/app/build.gradle. We will also add another dependency like this:

dependencies { implementation 'androidx.annotation:annotation:1.0.2' }

Add this code to the bottom of the line.

apply plugin: 'com.google.gms.google-services'

The last thing we need to do is to run flutter by using the following command:

flutter packages get

STEP 6: We will now do exactly the same for iOS. The steps involved are very similar.

We will set the package name; after that, we will get .plist file. We will add it to the iOS ios/Runner directory. This process has to be done in Xcode by opening the iOS project on it and dragging the file into Xcode so that it gets registered for inclusion during builds.

We do not need to add specific iOS dependencies for Firebase.

Conclusion

Firebase is a very powerful and beneficial backend platform for creating compelling applications for flutter. We can get access to many features, which is another reason why Firebase is preferred by so many developers worldwide. With Firebase's help, we can access databases, hosting platforms, different APIs, and many more.