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
- Go to Firebase Console.
- Click “Add project” → Enter your project name → Click Continue.
- 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:
- In Firebase Console, click Add app → Android.
- Enter:
- Android package name (found in
android/app/build.gradle→applicationId). - Nickname (optional).
- Android package name (found in
- Download the
google-services.jsonfile. - Place it inside:
android/app/google-services.json - Open
android/build.gradleand add:dependencies { classpath 'com.google.gms:google-services:4.4.2' } - Open
android/app/build.gradleand add at the bottom:apply plugin: 'com.google.gms.google-services'
For iOS:
- In Firebase Console → Add app → iOS.
- Enter your iOS bundle ID (from
ios/Runner.xcodeproj→Generaltab). - Download
GoogleService-Info.plist. - Add it to
ios/Runner/. - Open
ios/Runner/Info.plistand 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:
| Feature | Package |
|---|---|
| Authentication | firebase_auth |
| Cloud Firestore | cloud_firestore |
| Realtime Database | firebase_database |
| Cloud Storage | firebase_storage |
| Push Notifications | firebase_messaging |
| Analytics | firebase_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! 🎉