-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
Is there an existing issue for this?
- I have searched the existing issues.
Which plugins are affected?
Core
Which platforms are affected?
Web
Description
Bug Report
Description
When running Flutter Web with WASM compilation, accessing Firebase.apps
throws a JavaScriptError
, preventing proper Firebase initialization checks.
Environment
- Flutter Version: 3.32.8
- Firebase Core Version: 4.0.0
- Platform: Flutter Web (WASM)
- Browser: Chrome
Error Details
JavaScriptError
StackTrace: at module0.FirebaseCoreWeb.apps (http://localhost:3000/main.dart.wasm:wasm-function[63922]:0x775e6f)
at module0.Firebase.apps (http://localhost:3000/main.dart.wasm:wasm-function[63808]:0x772026)
at module0.FirebaseHelperService.initializeFirebase inner (http://localhost:3000/main.dart.wasm:wasm-function[63807]:0x771db4)
Problematic Code
// This throws JavaScriptError in WASM
if (Firebase.apps.isEmpty) {
app = await Firebase.initializeApp(options: options);
} else {
app = Firebase.app();
}
Workaround
The following approach works correctly in WASM:
FirebaseApp? app;
try {
app = Firebase.app();
} catch (e) {
app = await Firebase.initializeApp(options: options);
}
Expected Behavior
Firebase.apps
should be accessible in WASM environment without throwing JavaScript errors, allowing developers to check if Firebase is already initialized.
Actual Behavior
Accessing Firebase.apps
in WASM environment throws a JavaScriptError
and crashes the application.
Steps to Reproduce
- Create a Flutter Web app with WASM compilation enabled
- Add Firebase Core dependency
- Try to access
Firebase.apps.isEmpty
orFirebase.apps.length
- Observe the JavaScript error in browser console
Additional Context
This issue specifically affects WebAssembly compilation. The same code works fine in regular JavaScript compilation for Flutter Web.
Suggested Fix
Either:
- Fix the underlying issue with
Firebase.apps
in WASM environment, or - Update documentation to recommend the try/catch approach for WASM compatibility
Reproducing the issue
if (Firebase.apps.isEmpty) {
app = await Firebase.initializeApp(options: options);
} else {
app = Firebase.app();
}
Firebase Core version
4.0.0
Flutter Version
3.32.8
Relevant Log Output
JavaScriptError
StackTrace: at module0.FirebaseCoreWeb.apps (http://localhost:3000/main.dart.wasm:wasm-function[63922]:0x775e6f)
at module0.Firebase.apps (http://localhost:3000/main.dart.wasm:wasm-function[63808]:0x772026)
at module0.FirebaseHelperService.initializeFirebase inner (http://localhost:3000/main.dart.wasm:wasm-function[63807]:0x771db4)
Flutter dependencies
firebase_core: ^4.0.0
firebase_analytics: ^12.0.0
firebase_messaging: ^16.0.0
Additional context and comments
No response