Firebase.initializeApp() in Flutter
Problem
When integrating Firebase into a Flutter application, you may encounter the error:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
This error typically occurs when trying to use Firebase services (like Authentication, Firestore, etc.) before initializing the Firebase SDK. The error indicates that Firebase hasn't been properly set up before attempting to use its features.
Common symptoms include:
- App crashes when accessing Firebase services
- Authentication, database, or other Firebase operations fail
- The error appears in console logs when clicking buttons that trigger Firebase operations
Solution
1. Basic Initialization
The most straightforward solution is to initialize Firebase in your main.dart
file before running the app:
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
WARNING
Make sure to:
- Add the
async
keyword to themain()
function - Call
WidgetsFlutterBinding.ensureInitialized()
first - Use
await
beforeFirebase.initializeApp()
2. Using FutureBuilder for Better UX
For a better user experience, use FutureBuilder
to show a loading indicator while Firebase initializes:
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Firebase.initializeApp(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return ErrorWidget(); // Your error widget
}
if (snapshot.connectionState == ConnectionState.done) {
return MyApp(); // Your main app
}
return LoadingWidget(); // Your loading widget
},
);
}
}
3. Modern Approach with FlutterFire CLI (Recommended)
For Flutter 2.8+ versions, use the FlutterFire CLI for simplified configuration:
dart pub global activate flutterfire_cli
flutterfire configure
This generates a firebase_options.dart
file and handles platform-specific configuration.
Then use it in your main.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());
}
4. Platform-Specific Initialization
For specific platform requirements:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isIOS) {
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: "your-api-key",
appId: "your-app-id",
messagingSenderId: "your-sender-id",
projectId: "your-project-id",
),
);
} else {
await Firebase.initializeApp();
}
runApp(MyApp());
}
Common Issues and Fixes
Missing firebase_core Dependency
Ensure you have the latest firebase_core
package in your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
firebase_core: ^2.13.0
# Other Firebase packages
Incorrect Package Import
DANGER
Don't use the wrong package! Ensure you're importing the official Firebase package:
import 'package:firebase_core/firebase_core.dart'; // Correct
Not:
import 'package:firebase_core_dart/firebase_core_dart.dart'; // Incorrect
Web-Specific Configuration
For Flutter web, you need to include Firebase SDK in web/index.html
:
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js"></script>
<!-- Add other Firebase products as needed -->
<script>
var firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "..."
};
firebase.initializeApp(firebaseConfig);
</script>
Android Configuration
Ensure you have the Google services plugin in android/app/build.gradle
:
apply plugin: 'com.google.gms.google-services'
Initialization Order
WARNING
Ensure Firebase initialization completes before any other async operations:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(); // This first
await OtherService.initialize(); // Then others
runApp(MyApp());
}
Best Practices
- Initialize once: Call
Firebase.initializeApp()
only once in your application - Error handling: Wrap initialization in try-catch blocks for better error reporting
- Platform awareness: Use platform checks when different configurations are needed
- Dependency management: Keep Firebase packages updated to the latest versions
- Testing: Test initialization on all target platforms (iOS, Android, Web)
Migration from Legacy Setup
If migrating from older Firebase setups:
- Remove platform-specific config files (
google-services.json
,GoogleService-Info.plist
) - Remove related Gradle plugin entries
- Use the FlutterFire CLI for modern configuration
By following these guidelines, you should be able to resolve the "No Firebase App '[DEFAULT]' has been created" error and ensure proper Firebase initialization in your Flutter applications.