flutter
config_error
ai_generated
true
FirebaseException: 名为 '[DEFAULT]' 的 Firebase 应用已存在
FirebaseException: Firebase App named '[DEFAULT]' already exists
ID: flutter/firebase-app-not-initialized
95%修复率
90%置信度
1证据数
2024-04-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Flutter 3.22.0 | active | — | — | — |
| firebase_core 2.27.0 | active | — | — | — |
| firebase_auth 4.17.0 | active | — | — | — |
根因分析
多次调用 Firebase.initializeApp() 而未检查默认应用是否已初始化。
English
Calling Firebase.initializeApp() multiple times without checking if the default app is already initialized.
官方文档
https://firebase.flutter.dev/docs/overview解决方案
-
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.
无效尝试
常见但无效的做法:
-
90% 失败
Each call attempts to reinitialize, causing the 'already exists' error on subsequent calls.
-
80% 失败
Ignoring the error may lead to duplicate Firebase instances and unexpected behavior.
-
95% 失败
Firebase apps cannot be deleted programmatically; this approach is not supported.