# FirebaseException: Firebase not initialized. Make sure to call Firebase.initializeApp() before using any Firebase service.

- **ID:** `flutter/firebase-initialization-race`
- **Domain:** flutter
- **Category:** runtime_error
- **Error Code:** `FIREBASE_NOT_INITIALIZED`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A Firebase service method was called before Firebase.initializeApp() completed, often due to a race condition in asynchronous initialization.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.22.0 | active | — | — |
| firebase_core 2.24.0 | active | — | — |
| firebase_auth 4.16.0 | active | — | — |

## Workarounds

1. **Ensure Firebase.initializeApp() is called at the top level of main.dart before runApp(). Use await and wrap in a try-catch. Example: void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); }** (95% success)
   ```
   Ensure Firebase.initializeApp() is called at the top level of main.dart before runApp(). Use await and wrap in a try-catch. Example: void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); }
   ```
2. **Use a FutureBuilder or a splash screen that waits for initialization. Example: FutureBuilder(future: Firebase.initializeApp(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return MyApp(); } else { return SplashScreen(); } })** (90% success)
   ```
   Use a FutureBuilder or a splash screen that waits for initialization. Example: FutureBuilder(future: Firebase.initializeApp(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return MyApp(); } else { return SplashScreen(); } })
   ```
3. **If using multiple Firebase instances, call Firebase.initializeApp() with the specific options for each instance before accessing their services.** (85% success)
   ```
   If using multiple Firebase instances, call Firebase.initializeApp() with the specific options for each instance before accessing their services.
   ```

## Dead Ends

- **Call Firebase.initializeApp() synchronously by removing async/await.** — Firebase.initializeApp() is inherently asynchronous; removing await causes it to return a Future that may not complete before service calls. (95% fail)
- **Move Firebase.initializeApp() to the initState() of every widget that uses Firebase.** — Multiple calls to initializeApp() are not harmful but do not guarantee the timing; the race condition persists if widgets initialize independently. (80% fail)
