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

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



Solution 1:[1]

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 -).

Solution 2:[2]

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");
 });
}

Solution 3:[3]

flutter clean

and

flutter pub get

executing these two commands consecutively solved my problem.

Solution 4:[4]

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.

Solution 5:[5]

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,
)

Solution 6:[6]

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

Solution 7:[7]

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.

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 Ryan Heitner
Solution 2 K D
Solution 3 ilyashyrt
Solution 4 Fakhriddin Abdullaev
Solution 5 wohlstad
Solution 6 Community
Solution 7 Yves Boutellier