31

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.

K_Chandio
  • 558
  • 1
  • 7
  • 16

8 Answers8

45

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);
    
Quyen Anh Nguyen
  • 1,204
  • 13
  • 21
16

Adding a name param is to avoid the issue but does not fix the real issue.

In my case (Flutter), I've been using firebase_options.dart generated file while there were still some old google-services.json files inside each android flavor directories

android/app/src/<flavor>/google-services.json

and the same on ios:

ios/Runner/<flavor>/GoogleService-Info.plist

Deleting those old files solve the issue.

Make a 'Flutter clean' after and delete manually android/build if not deleted

Seb
  • 200
  • 1
  • 5
  • what are flavour directories? I'm having this problem as well but I only have one copy of the files you mentioned – Nathan Tew Jul 30 '22 at 12:55
  • @NathanTew See this article for flavors, which are for different settings per environment: https://kmtsandeepanie.medium.com/set-up-multiple-firebase-environments-in-flutter-9f88bc284454 – Justin Nov 08 '22 at 14:09
  • 1
    @NathanTew if you don't have any flavours setup, you can just delete the single files. – Pascal Gehring Dec 14 '22 at 14:24
  • Out of interest, how to you the set the firebase options to work with product flavors? – James Dec 16 '22 at 13:37
  • @James Have a look at this https://codewithandrea.com/articles/flutter-flavors-for-firebase-apps/ – GrahamD Jan 04 '23 at 12:24
  • I still needed a `flutter pub get` and a `flutterfire configure` after deleting, which created an up-to-date **GoogleService-Info.plist**. – andreas1724 Aug 02 '23 at 13:50
13

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.

Madushan
  • 6,977
  • 31
  • 79
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.

2

If you are using flutterfire tool to connect to firebase then the error may be fixed by deleting these files

android/app/src/<flavor>/google-services.json

ios/Runner/<flavor>/GoogleService-Info.plist

lib/firebase_options.dart

then flutter clean and flutter pub get should fix the issue

  • 1
    I had the error on mac with the ios simulator. The GoogleService-Info.plist was outdated. I changed to my prod firebase project with flutterfire configure --project myfirebaseprojectname, but this cmd did not touch the GoogleService-Info.plist for ios, therefore I just deleted this file and then ran the above flutterfire configure command and then it created (among others) the correct file for ios. – Gerros Apr 21 '23 at 11:47
0

This worked for me

if (Platform.isAndroid) {
        await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform,
        );
      } else if (Platform.isIOS) {
        await Firebase.initializeApp();
      }
0

Try

1) flutter clean & flutter pub get

2) Deleting google-services.json and download new google-services.json from firebase. If you are using cli run fluttefire configureone more.

Rishal
  • 194
  • 5
0

if you are using flutterfire you should match FirebaseOptions and google-services.json

firebase_options.dart

static const FirebaseOptions android = FirebaseOptions(
    apiKey: 'your_api_ky',
    appId: 'your_api_id',
    messagingSenderId: '...',
    projectId: '...',
    storageBucket: '...',
  );

google-services.json

"project_info": {
    "project_number": "...",
    "project_id": "...",
    "storage_bucket": "..."
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "your_api_id",
        "android_client_info": {
          "package_name": "com.app_your"
        }
      },
      "oauth_client": [
        {
          "client_id": "",
          "client_type": 3
        }
      ],