Connecting your Flutter App to Firebase

Connecting your android app to online databases is the next step for any android developer out there. But managing huge databases of your users is not a cup of cake. So rather than learning databases and back-end, you can use Firebase for your app and focus more on your app development.

What is Firebase ?

Firebase is Google’s mobile platform that helps you quickly develop high-quality apps and grow your business without worrying about how to manage your app data, users , etc. Firebase provides various services like Authentication, Storage, Real time Database and much more.

So in order to use Firebase you have to create account in Firebase and then you have to integrate your Firebase account with your app.
So in this blog, I will be showing integration of Flutter app with Firebase.

Here we go :

Step 1: First of all, go to Firebase website and create your account or sign in if already there. You can also easily use your google account to make Firebase account .

This is the first initial page loads up when you create a new acount.

Step 2: Click on Create a Project to create a new project for your app.

A Firebase project is the top-level entity for Firebase. In a project, you create Firebase apps by registering your iOS, Android, or web apps. After you register your apps with Firebase, you can add the Firebase SDKs for any number of Firebase products, like Analytics, Cloud Firestore, Performance Monitoring, or Remote Config.

  • Give a suitable name to your Project and click on Continue to go to Step 2 of Creating a Project.

Google Analytics enables you to understand your app users more efficiently and very easily like who are using your app, what actions they perform and a lot of other statistics about your app which you can use to improve your app to grow your bussiness.

  • Now enable Google Analytics for your project and hit Continue to come to Step 3 of creating a project.
  • Now Select a location for your analytics and accept all terms and conditions. Then hit Continue to create your project.

After these steps, your project will be created successfully.

Now click on continue and you will be redirected to your Project.

Now you have to just register your app with Firebase

Step 3: Now create a flutter project using below command

flutter create --org <domain> <project_name>

Step 4: Click on Android symbol in your project.

  • In Register app, enter the android package name of your app. It is given format <domain.projectName>.

You can also cross-check this in build.gradle file. It is present in android/app/build.gradle as highlighted below.

Then Click on Register app to move to next step.

  • Now download google-services.json file in
    <project-name>/android/app .

Note: The directory of this file should be fixed as mentioned above.

  • The next step is also very crucial as you have to add few lines of code to add firebase sdk in your app.
  1. In project-level build.gradle file( <project-name>/ android / build.gradle ) add these lines of code.
classpath 'com.google.gms:google-services:4.3.3'

It should be like this

Note: Add google() repository also if not present as highlighted above

2. In App-level build.gradle file (<project-name>/ android / app / build.gradle ) add these lines as shown below in samples

apply plugin: 'com.android.application' //Already present
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation 'com.google.firebase:firebase-analytics:17.5.0'
}

It should look like this

Note: The highlighted areas should be present in your gradle files. I would also suggest you to change minSdkVersion from 16(default) to 21 to avoid any complication later.

After all the changes, save the changes you made and click on Next.

  • Now it will wait for verification process.

Step 4: Now add Firebase database package in your pubspec.yaml file as shown below ( <project-name>/pubspec.yaml )

dependencies:
flutter:
sdk: flutter
firebase_database :

and also import firebase package in your main.dart file and write below lines in main function to use firebase.

import 'package:firebase_core/firebase_core.dart';Future <void> main() async { WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

It will look like this

Step 5: That’s it . Now run your app and it will run without any error.

and firebase console verification will also complete

Now you can click on Continue to Dashboard and can integrate Firebase products like authentication, firestore, etc with your flutter app by importing and downloading respective packages.

So that’s it folks for this blog. Hope you liked it. See you in next blog.

LinkedIn Profile: https://www.linkedin.com/in/apeksh-agarwal-0543bb192/

Github Profile: https://github.com/Apeksh742