Connecting your Flutter Project to Firebase For Both Android And Ios

Firebase can be essential while building apps and the first important step is to know how to connect it to our flutter projects. In this article, I will show you how to do that in a few steps. What you need to know before reading this article

  1. How to create a Flutter project.
  2. How to create a project in Firebase Console.

Let's get into it. First, create your flutter project locally and create a firebase project in the firebase console. Open the flutter project in either android studio or vs code and open the terminal or you can also open cmd in the root directory of your flutter project. Install the firebase plugin by running:

flutter pub add firebase_core

this code on your terminal. Now let's install the flutter fire cli by running

dart pub global activate flutterfire_cli

Notice You will be prompted to add the flutter cli to your machine path

Warning: Pub installs executables into C:\Users\PC\AppData\Local\Pub\Cache\bin, which is not on your path. You can fix that by adding that directory to your system's "Path" environment variable. A web search for "configure windows path" will show you how.

Ensure you have added the flutter Cli to your path and then proceed. Next step is to configure flutter by running this code

flutterfire configure

Follow the prompts and select the firebase project that you created, and add both ios and android and this will automatically connect your ios and android flutter projects to firebase. Once configured, a > firebase_options.dart file will be generated for you containing all the options required for initialization.

The last step is initializing Firebase in our project. Do this in the main.dart file as shown below:

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

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