Integrating Firebase with Flutter is one of the most common tasks for building modern apps (authentication, database, push notifications, etc.). Here’s a step-by-step guide you can follow.
🔥 Step-by-Step: Firebase Integration with Flutter
✅ 1. Create Firebase Project
- Go to Firebase Console
- Click “Add Project”
- Enter project name → Continue → Create project
📱 2. Add App to Firebase
For Android:
- Click Add App → Android
- Enter:
- Package name (from Flutter project)
- App nickname (optional)
- Download google-services.json
- Place it in:
android/app/google-services.json
For iOS (Optional):
- Click Add App → iOS
- Enter Bundle ID
- Download GoogleService-Info.plist
- Add it to:
ios/Runner/
⚙️ 3. Install Firebase CLI (Recommended)
Install CLI:
npm install -g firebase-tools
Login:
firebase login
⚡ 4. Add Firebase to Flutter (Easiest Method)
Use FlutterFire CLI:
dart pub global activate flutterfire_cli
flutterfire configure
This will:
- Connect your app to Firebase
- Generate
firebase_options.dart
📦 5. Add Required Dependencies
In pubspec.yaml:
dependencies:
flutter:
sdk: flutter
firebase_core: latest_version
Then run:
flutter pub get
🚀 6. Initialize Firebase in Flutter
Update 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(MyApp());
}
🔐 7. Example: Add Firebase Authentication
Add dependency:
firebase_auth: latest_version
Example login:
import 'package:firebase_auth/firebase_auth.dart';Future<void> signIn() async {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: "test@email.com",
password: "123456",
);
}
🔥 8. Other Firebase Services You Can Use
- 🔑 Authentication (Email, Google, Phone)
- ☁️ Firestore Database
- 📦 Firebase Storage
- 🔔 Push Notifications (FCM)
- 📊 Analytics
🧪 9. Run Your App
flutter run
If everything is correct → Firebase is successfully connected ✅
⚠️ Common Errors & Fixes
❌ “Firebase not initialized”
✔ Ensure:
await Firebase.initializeApp();
❌ Gradle Error (Android)
✔ Add in android/build.gradle:
classpath 'com.google.gms:google-services:4.3.15'
✔ In android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'
❌ SHA-1 Missing (for Google Login)
✔ Add SHA-1 in Firebase project settings
🎯 Pro Tips
- Use FlutterFire CLI (saves a lot of time)
- Keep Firebase rules secure (Firestore & Storage)
- Use environment configs for production apps
0 Comments