November 7, 2025
Flutter

How to use firebase in flutter

Here’s a step-by-step guide on how to use Firebase in a Flutter app, including setup, installation, and basic usage (authentication, database, etc.).


🔧 Step 1: Create a Firebase Project

  1. Go to Firebase Console.
  2. Click “Add project” → Enter your project name → Click Continue.
  3. Disable Google Analytics (optional) → Click Create project.

🔗 Step 2: Add Firebase to Your Flutter App

You’ll connect your Flutter project (Android/iOS/Web) to Firebase.

For Android:

  1. In Firebase Console, click Add app → Android.
  2. Enter:
    • Android package name (found in android/app/build.gradleapplicationId).
    • Nickname (optional).
  3. Download the google-services.json file.
  4. Place it inside: android/app/google-services.json
  5. Open android/build.gradle and add: dependencies { classpath 'com.google.gms:google-services:4.4.2' }
  6. Open android/app/build.gradle and add at the bottom: apply plugin: 'com.google.gms.google-services'

For iOS:

  1. In Firebase Console → Add app → iOS.
  2. Enter your iOS bundle ID (from ios/Runner.xcodeprojGeneral tab).
  3. Download GoogleService-Info.plist.
  4. Add it to ios/Runner/.
  5. Open ios/Runner/Info.plist and ensure it includes: <key>FirebaseAppDelegateProxyEnabled</key> <true/>

⚙️ Step 3: Add Firebase SDK to Flutter

In your Flutter project’s pubspec.yaml, add:

dependencies:
  firebase_core: ^3.6.0

Then run:

flutter pub get

🚀 Step 4: Initialize Firebase

Open lib/main.dart and initialize Firebase before running the app:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(child: Text('Firebase Connected Successfully!')),
      ),
    );
  }
}

🔥 Step 5: Add Firebase Features

Depending on what you want to use, add these packages:

FeaturePackage
Authenticationfirebase_auth
Cloud Firestorecloud_firestore
Realtime Databasefirebase_database
Cloud Storagefirebase_storage
Push Notificationsfirebase_messaging
Analyticsfirebase_analytics

Example (add in pubspec.yaml):

dependencies:
  firebase_auth: ^5.1.0
  cloud_firestore: ^5.4.3

Run:

flutter pub get

👤 Example: Firebase Authentication

import 'package:firebase_auth/firebase_auth.dart';

// Sign up
Future<void> signUp(String email, String password) async {
  await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: email,
    password: password,
  );
}

// Sign in
Future<void> signIn(String email, String password) async {
  await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email,
    password: password,
  );
}

// Sign out
Future<void> signOut() async {
  await FirebaseAuth.instance.signOut();
}

📚 Example: Firestore CRUD

import 'package:cloud_firestore/cloud_firestore.dart';

final db = FirebaseFirestore.instance;

// Add data
Future<void> addUser() async {
  await db.collection("users").add({"name": "John", "age": 25});
}

// Read data
Stream<QuerySnapshot> getUsers() {
  return db.collection("users").snapshots();
}

// Update data
Future<void> updateUser(String id) async {
  await db.collection("users").doc(id).update({"age": 30});
}

// Delete data
Future<void> deleteUser(String id) async {
  await db.collection("users").doc(id).delete();
}

✅ Step 6: Run the App

Finally, run:

flutter run

Your app is now connected to Firebase! 🎉

Related posts

What is bloc and how to use in flutter application step by step guide

Dheeraj Pal

How to create a simple quiz app using flutter

Dheeraj Pal

How to make Diary App using Flutter and Getx

Dheeraj Pal

Leave a Comment