flutter
config_error
ai_generated
true
FirebaseException: Firebase App named '[DEFAULT]' already exists
ID: flutter/firebase-app-not-initialized
95%Fix Rate
90%Confidence
1Evidence
2024-04-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Flutter 3.22.0 | active | — | — | — |
| firebase_core 2.27.0 | active | — | — | — |
| firebase_auth 4.17.0 | active | — | — | — |
Root Cause
Calling Firebase.initializeApp() multiple times without checking if the default app is already initialized.
generic中文
多次调用 Firebase.initializeApp() 而未检查默认应用是否已初始化。
Official Documentation
https://firebase.flutter.dev/docs/overviewWorkarounds
-
95% success Check if Firebase is already initialized before calling initializeApp: `if (Firebase.apps.isEmpty) { await Firebase.initializeApp(); }`.
Check if Firebase is already initialized before calling initializeApp: `if (Firebase.apps.isEmpty) { await Firebase.initializeApp(); }`. -
90% success Initialize Firebase only once in the main() function before runApp: `WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp());`.
Initialize Firebase only once in the main() function before runApp: `WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp());`.
-
85% success Use a singleton pattern to wrap Firebase initialization and prevent duplicate calls.
Use a singleton pattern to wrap Firebase initialization and prevent duplicate calls.
中文步骤
Check if Firebase is already initialized before calling initializeApp: `if (Firebase.apps.isEmpty) { await Firebase.initializeApp(); }`.Initialize Firebase only once in the main() function before runApp: `WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp());`.
Use a singleton pattern to wrap Firebase initialization and prevent duplicate calls.
Dead Ends
Common approaches that don't work:
-
90% fail
Each call attempts to reinitialize, causing the 'already exists' error on subsequent calls.
-
80% fail
Ignoring the error may lead to duplicate Firebase instances and unexpected behavior.
-
95% fail
Firebase apps cannot be deleted programmatically; this approach is not supported.