Problem
When working with Flutter and Firebase, you may fall into the following error:
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
Don’t panic. We will tackle this bug.
Solution
Before using any Firebase services, you have to initialize the Firebase app by calling Firebase.initializeApp(). Only one time, no more, no less.
The solution to fix the mentioned problem and make sure it won’t happen again is quite simple. All you need to do is open the main.dart file and change from this:
void main(){
runApp(MyApp());
}
To this:
// Import the Firebase Core package
import 'package:firebase_core/firebase_core.dart';
// Add async/await and 2 lines of code to the main function
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
If you call Firebase.initializeApp() somewhere else, remove it. After that, completely restart your Flutter app and check the result.
Further reading:
- Flutter: Firebase Remote Config example
- Flutter + Firebase Storage: Upload, Retrieve, and Delete files
- Flutter: Configure Firebase for iOS and Android
- Flutter and Firestore Database: CRUD example
- Flutter: Correctly adding GoogleServices-Info.plist to iOS
You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.