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

Viewed 7738

Since im new in firebase, im having trouble with duplicated name already exists, and i need your help.

E/flutter (21802): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists
E/flutter (21802): #0      MethodChannelFirebase.initializeApp
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:111
E/flutter (21802): <asynchronous suspension>
E/flutter (21802): #1      Firebase.initializeApp
package:firebase_core/src/firebase.dart:41
E/flutter (21802): <asynchronous suspension>
E/flutter (21802): #2      main
package:flutterbuyandsell/main.dart:47
E/flutter (21802): <asynchronous suspension>
E/flutter (21802):

…/method_channel/method_channel_firebase.dart:111

Method_channel_firebase

package:firebase_core/src/firebase.dart:41

Firebase.dart

package:flutterbuyandsell/main.dart:47

firebase on main.dart

8 Answers

I solved the issue by adding a name to the initializeApp method:

await Firebase.initializeApp(
  name: 'name-here',
  options: DefaultFirebaseOptions.currentPlatform,
)

I used the same name used in the firebase console as my project ID for consistency, keep in mind that you cannot add spaces to the name (only dashes -).

Add this in Flutter

NOTE - Use this "Firebase.initializeApp" only Once in the entire Project.

// Wait for Default Firebase app to initialize

if (Firebase.apps.isEmpty) {
  await Firebase.initializeApp(
  name: 'YourAPP',
  options: DefaultFirebaseOptions.currentPlatform,
 ).whenComplete(() {
  print("completedAppInitialize");
 });
}

I solved this issue by adding the name of my Firebase project. You can try this:

await Firebase.initializeApp(
  name: 'name of your App in FireStore',
  options: DefaultFirebaseOptions.currentPlatform,
)
flutter clean

and

flutter pub get

executing these two commands consecutively solved my problem.

You can solve this problem by changing your main.dart file as below:

void main() async {
  await Firebase.initializeApp(
      name: '<YourAppName>',
      options: FirebaseOptions(
          apiKey: '<apiKey>',
          appId: '<appId>',
          messagingSenderId: '<senderId>',
          projectId: '<projectId>'));

  runApp(MyApp());
}

This worked for me, I believe it helps you.

 if (Firebase.apps.isEmpty) {
    await Firebase.initializeApp();
 }

if you make changes to your options while you run the app with flutter run -d chrome and you use just hot reload after you made your changes to the options, it tries to initialise a second app, which doesn't work. Just terminate your running session with ctrl+c and start a fresh session, the error will be gone.

I have two solutions which I have tested personally and they both worked:

First Solution:

Just clean the project using flutter clean and run it again, it should now work!


Second Solution:

  1. Comment the Initialization of Firebase in the main method.
  2. Run the App.
  3. Once the app is running, stop it.
  4. Uncomment the same initialization code.
  5. Run the App.

That's it

Related