flutter config_error ai_generated true

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

ID: flutter/firebase-app-not-initialized

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2024-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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/overview

Workarounds

  1. 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(); }`.
  2. 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());`.
  3. 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.

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 80% fail

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

  3. 95% fail

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