Flutter Firebase Tutorial - Getting Started

Firebase is a backend service managed by Google and is hugely used by developing teams for fast and easy development removing the hassle of setting up and running their own servers.

So in this article, we are going to learn to set up our flutter project for Firebase.

Create Flutter Project

First, we need to create a flutter project in order to proceed with the Firebase setup. Create a new project by entering any project name in place of project_name using the following command or you can use Android Studio.

flutter create firebase_tutorial

After the project is created, move to the next setup.

Create a new project in the Firebase console

Open the link https://console.firebase.google.com/ and then log in or create an account if you don't have an account. After successful login, you should receive the following screen.

Click on Add Project provide the project name and click on Continue.

Next check if you want Google Analytics or not and click on continue.

In the last step of creating the project, select the Google Analytics account. Finally, click on Create Project.

It would take a minute or two and then your project would be created.

Lastly a continue button will appear, click on it to proceed.

Add Firebase to your application

For adding an application, you need to create that in the dashboard first. So proceed as follows:

Head over to Project Settings beside Project Overview.

Under the General tab, rhere is your Apps Section. You can manage the applications(IOS, Android, Web, Flutter or Unity3D). Select the Flutter icon from there.

Follow the given steps provided in the window.

You need to install Firebase CLI. Follow the steps here.

Then run the following command.

firebase login

Then a prompt will open on the browser. Follow the steps provided.

Move to the project folder and run the following commands(also provided in the tutorial)

dart pub global activate flutterfire_cli
flutterfire configure --project=CHANGE_THE_PROJECT_ID

This automatically registers your per-platform apps with Firebase and adds a lib/firebase_options.dart configuration file to your Flutter project.

In the pubspec.yaml file, add the firebase_core plugin.

dependencies:
    firebase_core:

Finally in the main.dart file, initialize Firebase as follows:

main.dart

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

We have successfully created and set up our application as well.

In the next tutorials, we are going to explore other Firebase features.

Did you find this article valuable?

Support All About Flutter | Flutter and Dart by becoming a sponsor. Any amount is appreciated!