Search code examples
flutterfirebasefirebase-authentication

Flutter web build cause 'No Firebase App '[DEFAULT]' has been created - call' in new screen


I call this to initialize Firebase.

Future initializeFirebaseApp() async {
  await Firebase.initializeApp(
    name: DefaultFirebaseOptions.firebaseName,
    options: DefaultFirebaseOptions.currentPlatform,
  )
      .whenComplete(() => Log().i('Firebase Initialized'))
      .onError((error, stackTrace) {
    Log().e('Firebase Initialize Error: $error $stackTrace');
    return Future.error("");
  });
}
fun main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeFirebaseApp();

  runApp(const ProviderScope(child: IAmApp()));
}

And I use AutoRouter.

When I change the screen with context.pushRoute(const PasswordLoginRoute())

And PasswordLoginScreen has FirebaseAuth like this in HookConsumerWidget:

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final firebaseAuth = ref.watch(firebaseAuthProvider);
    // ...
  }
@riverpod
FirebaseAuth firebaseAuth(FirebaseAuthRef ref) => FirebaseAuth.instance;

And because of that I get No Firebase App '[DEFAULT]' has been created - call.

Should I initialize Firebase every page for web build?


Solution

  • It looks like you passed a custom name when initializing Firebase.

    To resolve this do either one of these;

    1. Remove the custom name and allow Firebase to initialize the app with [DEFAULT] name;
    await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      )
    
    1. Retrieve your custom-named Firebase app whenever you need to use any Firebase service.
    final firebaseAuth = FirebaseAuth.instanceFor(app: Firebase.app(DefaultFirebaseOptions.firebaseName));