How To Add Firebase To Your Flutter App? cover image

How To Add Firebase To Your Flutter App?

AZRAF AL MONZIM
by

Published on

In this lesson, we are going to learn how we can add Firebase to our flutter application. And we are going the follow the official documentation.

# create a flutter app run this command
flutter create app_name --org com.YOUR_ORG_NAME

Step 2: Create a Firebase Console Account

Visit the Firebase Console and log in with your Google account. Optionally, create a new project with a unique name.

Step 3: Open Flutter Project in IDE

Open your Flutter project in your preferred IDE (e.g., VSCode).

Prerequisites

Make sure you have the following installed:

  • Firebase CLI
  • Flutterfire CLI

Already setup firebase and flutterfire cli then skips (1.0-2.0)

  1. Install Firebase CLI

    # To follow the next Step you must npm or yarn installs your system. (install npm)
    npm install -g firebase-tools

    For Linux:

    curl -sL https://firebase.tools | bash
  2. Configure Firebase CLI

    Log in to your account with Firebase CLI:

    firebase login

    It will open a new tab in chrome select the account and give the permission. If you successfully log in then flow to the next step.

  3. Install Flutterfire CLI:

    dart pub global activate flutterfire_cli

Step 4: Initialize Firebase in Your App

Open your project in VSCode and navigate to the root directory. Run the following commands in the terminal:

  1. From your Flutter project directory, run the following command to install the core plugin:

    flutter pub add firebase_core
  2. Ensure your Flutter app's Firebase configuration is up-to-date

    flutterfire configure
  3. In your lib/main.dart file, import the Firebase core plugin and the configuration file you generated earlier:

    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
  4. Also in your lib/main.dart file, initialize Firebase using the DefaultFirebaseOptions object exported by the configuration file:

    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
  5. Rebuild your Flutter application:

    flutter run

For more details, refer to the official Firebase CLI documentation and Flutter setup guide.