Home > Back-end >  Firebase.initializeApp name parameter is needed in mobile but not in web
Firebase.initializeApp name parameter is needed in mobile but not in web

Time:01-01

I have a Flutter app that works for Android, iOS and web. I use Firebase in main like this:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

  try {
    await Firebase.initializeApp(
      // name: 'name',
      options: DefaultFirebaseOptions.currentPlatform,
    );

    runApp(
      MultiProvider(
        providers: [
          // providers
        ],
        child: const MyApp(),
      ),
    );
  } catch (e) {
    print(e.toString());
  }
}

When the parameter name is set, it runs in both Android and iOS, but in web it doesn't run. The window is created but stays white, and the console says:

[core/no-options] Firebase: Need to provide options, when not being deployed to hosting via source..

When I comment the same parameter, it runs in web, but I have the same problem in Android and iOS. It says:

flutter: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists

How can I make it work for both?

CodePudding user response:

You could check if the platform is web using kIsWeb and set name only if it's not web

if(kIsWeb){
 await Firebase.initializeApp(
   await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
 );
}else{
    await Firebase.initializeApp(
      name: 'name',
      options: DefaultFirebaseOptions.currentPlatform,
    );
} 
  • Related