'A Firebase App named "[DEFAULT]" already exists

I have my flutter App integrated with firebase, everything was fine but when I migrated firebase project to client firebase console, added his google services file, changed DefaultFirebaseOption.currentplatform file credentials but I got error whenever I try to run my app. My main method looks like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  token = await FirebaseMessaging.instance.getToken();  
  Provider.debugCheckInvalidValueType = null;
  runApp(const MyApp());
}

The error is:

E/flutter (28330): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists.

I searched here and found a solution from here and updated my main method like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
   if (Firebase.apps.isNotEmpty) {
     await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
   }else{
     Firebase.app()
   }
  //await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  token = await FirebaseMessaging.instance.getToken();  
  Provider.debugCheckInvalidValueType = null;
  runApp(const MyApp());
}

but this time I got no error but my app UI is not showing, I just see black screen. I have been trying to solve this from 4 to 5 hours but found no solution.any Help will highly be appreciated.

Update I provide name parameter in both of the scenrios and my app worked fine for first time i install, but when i retart or close app and re run it, I got same error as mentioned in above cases.



Solution 1:[1]

I had similar issue when create firebase app by flavors using flutterfire CLI. I realized that flutterfire create apps with same name in different projects. Then I tried 2 ways and it works.

  • 1st: manually change app name in each firebase project.

  • 2nd: add param name when initialize such as in main_dev.dart

     await Firebase.initializeApp(
     name: "dev project",
     options: DefaultFirebaseOptions.currentPlatform);
    

Solution 2:[2]

Got this error after fumbling with firebase projects and upgrading packages.

Fixed by

flutter clean
flutter pub get

then build the project again.

I have apps with same name in different projects, this doesn't seem to cause the error.

Solution 3:[3]

In my case incorrect GoogleService-Info.plist was linked in xCode from downloads folder. But correct was in Runner folder, so i thought that its ok. So check in Build Phases -> Copy Bundle Resources that GoogleService-Info.plist exists and path is correct.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quyen Anh Nguyen
Solution 2 Madushan
Solution 3 Lygin Andrew