flutter config_error ai_generated true

FirebaseException: 名为 '[DEFAULT]' 的 Firebase 应用已存在

FirebaseException: Firebase App named '[DEFAULT]' already exists

ID: flutter/firebase-app-not-initialized

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://firebase.flutter.dev/docs/overview

解决方案

  1. Check if Firebase is already initialized before calling initializeApp: `if (Firebase.apps.isEmpty) { await Firebase.initializeApp(); }`.
  2. Initialize Firebase only once in the main() function before runApp: `WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp());`.
  3. Use a singleton pattern to wrap Firebase initialization and prevent duplicate calls.

无效尝试

常见但无效的做法:

  1. 90% 失败

    Each call attempts to reinitialize, causing the 'already exists' error on subsequent calls.

  2. 80% 失败

    Ignoring the error may lead to duplicate Firebase instances and unexpected behavior.

  3. 95% 失败

    Firebase apps cannot be deleted programmatically; this approach is not supported.