Monday, April 4, 2022

Flutter Tutorial: How to Add Firebase to Flutter Project (2022 Update)

In this article, we will discuss how to add Firebase in our flutter based application.

Create and Prepare the Firebase Project

  • Go to Google Firebase and create a project. You will need a Gmail E-Mail to access Firebase.
  • Navigate to the console page of Firebase. Create a new project by clicking on add project.
  • After clicking the button you will be take to a new page. The project creation has two steps.
  • Give your project a name
  • Add Firebase Analytics to your project. I recommend disabling this option unless you really need it.
  • Finish the creation of your project by clicking *Create Project*

After creating your project you will be taken to the project overview. Unlike in previous times when adding Firebase to Flutter you are done inside the Firebase console now.

We also need firebase-cli installed on our computer. So for those who have not installed firebase-cli, please install it first according to their respective OS.

Insert Firebase to Flutter Project

Create a Flutter project or use your desired project. To add the Firebase Core Package to your pubscpec run this command:

flutter pub add firebase_core 

Also run the following three commands

dart pub global activate flutterfire_cli 
//In case you get an error or warning after command 1: export 
flutterfire configure

If you have done everything correctly the following file will be created: firebase_options.dart. Import those file inside your main.dart by adding this line:

import ‘firebase_options.dart’;

and add this syntax to add firebase function

  void main() async { 
    WidgetsFlutterBinding.ensureInitialized(); await 
    Firebase.initializeApp( options: 
      DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp());
That’s it. Your Firebase has been added.


Post a Comment