Skip to content

firebase_core: Firebase.apps throws JavaScriptError in WebAssembly (WASM) environment #17638

@raphaexa

Description

@raphaexa

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

  1. Create a Flutter Web app with WASM compilation enabled
  2. Add Firebase Core dependency
  3. Try to access Firebase.apps.isEmpty or Firebase.apps.length
  4. 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:

  1. Fix the underlying issue with Firebase.apps in WASM environment, or
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked: customer-responseWaiting for customer response, e.g. more information was requested.platform: webIssues / PRs which are specifically for web.plugin: coretype: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions