Perfect Solution – No Firebase App ‘[DEFAULT]’ has been created – Flutter Error
Are you building a Flutter application and have integrated Firebase, but keep getting an error while clicking on the register, login, or logout button? Several people might have answered the same question, but none seems to work for you. Wondering how to fix this “No Firebase App ‘[DEFAULT]’ has been created” problem?
Problem
You are using the firebase method in the app and you are getting an exception like this.
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following FirebaseException was thrown while handling a gesture:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
Now let’s talk about the solutions.
Solutions
First of all, you need to update all Firebase versions and call Firebase.initializeApp() before using any Firebase product.
Now, Firebase products depend on the firebase_core version (0.5.0+). Thus, you must add it in the pubspec.yaml file. For instance:
dependencies:
flutter:
sdk: flutter
# firebase dependencies
firebase_core: ^2.8.0 #--------------you need to add version above 0.5.0+
firebase_auth: ^4.3.0
firebase_messaging: ^14.3.0
cloud_firestore: ^4.4.5
firebase_crashlytics: ^3.0.17
firebase_storage: ^11.0.16
Then you have to call Firebase.initializeApp(). I have given you some ways to call firebase dependencies, you can use any one that will suitable for your app.
1st Solution – Recommended
If you are not using FutureBuilders then you need to just Initialize it in the main() method after calling WidgetsFlutterBinding.ensureInitialized();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
2nd Solution
Initialize it in initState() then call setState() which will call the build() method. But remember, call this method before using any firebase functionalities.
@override
void initState() {
super.initState();
Firebase.initializeApp().whenComplete(() {
print("completed");
setState(() {});
});
}
3rd Solution
You can use FutureBuilder to initialize firebase app in the first widget of the app. The following code will help you initialize it. But still, I prefer you to initialize the firebase app to the main function.
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire
future: Firebase.initializeApp(),
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return ErrorScreen();
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return HomeScreen();
}
// Otherwise, show something whilst waiting for initialization to complete
return CircularProgressIndicator();
},
);
}
}
Note: You only have to call initializeApp() once. You can read more about firebase here.
Conclusion
In conclusion, the “No Firebase App ‘[DEFAULT]’ has been created” error is a common issue that can occur while working with Firebase in Flutter. However, by following the steps outlined in this article, you can easily resolve the error and get back to developing your app.
Remember to ensure that your Firebase configuration is correct, the necessary dependencies are installed, and the initialization code is written correctly. With these steps, you can troubleshoot the error and continue building your Flutter app with Firebase integration.
Check out other blog posts as well to get more solutions and knowledge about flutter technology.