No Firebase App has been created - call Firebase.initializeApp()
Asked Answered
A

8

11

I am trying to run my app and it is giving the following error

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

I have already called firebase.initializeApp() but still the error is same. here is my code of main file

    void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => AppointmentsProvider(),
      child: MaterialApp(
        title: 'Car Wash App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
         
        ),
        home: FutureBuilder(
          future: Firebase.initializeApp(),
          builder: (ctx, snap) =>
              snap.connectionState == ConnectionState.waiting
                  ? Center(
                      child: Text('Loading...'),
                    )
                  : StreamBuilder(
                      stream: FirebaseAuth.instance.authStateChanges(),
                      builder: (ctx, snapShot) =>
                          snapShot.hasData ? HomePage() : UserAuth(),
                    ),
        ),
        routes: {
          HomePage.routeName: (_) => HomePage(),
          HistoryPage.routeName: (_) => HistoryPage(),
          MyAppointments.routeName: (_) => MyAppointments(),
          PackagesPage.routeName: (_) => PackagesPage(),
          VehicleType.routeName: (_) => VehicleType(),
          SlotPage.routeName: (_) => SlotPage(),
          ExtraServicesPage.routeName: (_) => ExtraServicesPage(),
          SummaryPage.routeName: (_) => SummaryPage(),
          DetailedScreen.routeName: (_) => DetailedScreen(),
          PaymentMethodScreen.routeName: (_) => PaymentMethodScreen(),
        },
      ),
    );
  }
}

Any kind of help is much appreciated. Thank you

Ammerman answered 26/12, 2021 at 13:2 Comment(2)
Did the answer of Baimam Boukar help you?Jame
it gives this error "[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized."Ammerman
A
4

If the above answers don't help, you can solve the problem by following steps:

delete the project and take backup of lib folder and pubspec.yaml file.
Create a new project with the same name
copy backed up files to the newly created project.
run flutter pub get
you are good to go

It literally solved my problem. I hope this will help.

Amerce answered 28/12, 2021 at 11:26 Comment(0)
E
14

You should initialise it in main().

For example:

import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(MyApp());
}

This link might be helpful for you.

EDIT: The first link has been archived. This is new way to setup firebase in a Flutter project. Steps are pretty same. Last link contains one more step to configure firebase plugins.

Steps are:

Erme answered 26/12, 2021 at 13:53 Comment(3)
it is giving this error: Undefined name 'DefaultFirebaseOptions'.Ammerman
Hi @AdilNaseem have you followed the documentation steps? first step: dart pub global activate flutterfire_cli , second step: flutterfire configureand this step will create a file named firebase_options.dartand you should put it into your project file and call it from main.dart.Erme
when I try this command in step -1, it gives this error "'dart' is not recognized as an internal or external command, operable program or batch file."Ammerman
A
5

Firebase.initializeApp() is an asynchronous function. You should use "await" to ensure Firebase initialization. And use WidgetsFlutterBinding.ensureInitialized() to prevent flutter errors.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}
Aristate answered 26/12, 2021 at 14:12 Comment(0)
A
4

If the above answers don't help, you can solve the problem by following steps:

delete the project and take backup of lib folder and pubspec.yaml file.
Create a new project with the same name
copy backed up files to the newly created project.
run flutter pub get
you are good to go

It literally solved my problem. I hope this will help.

Amerce answered 28/12, 2021 at 11:26 Comment(0)
D
3

The problem is that you have configure firebase in your flutter project but you have not initialized it before the app starts.

You need to initialize the firebase sdk asynchronously. This is how to go about it.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}
Divinity answered 26/12, 2021 at 15:7 Comment(1)
it gives this error "[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized."Ammerman
B
2

This error is know to occur when you fail to add the required dependencies in both your android side of things in project and app level build.gradle files

Bougainville answered 3/3, 2022 at 8:12 Comment(0)
H
2
  1. Check your android/build.gradle file. Inside dependecies add

    classpath 'com.google.gms:google-services:4.3.14' (use latest version)

  2. Go to app/build.gradle file. Add the plugin outside dependencies

    apply plugin: 'com.google.gms.google-services'

Herby answered 29/12, 2022 at 18:10 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Pulmonate
E
1

  1. Install this package

   For installing the Firebase core package Click here

   In your main.dart:-

    2. Import Firebase_core package :    

import 'package:firebase_core/firebase_core.dart';

3. Add these lines in your main :  

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(myApp());
}

It worked for me hoping helpful for others too.

Extravaganza answered 11/2, 2023 at 15:26 Comment(0)
L
0

In summary there are two solutions

  • make sure you have called Firebase.initializeApp before you use firebase
  • Or you have overridden the name of app other than default name (This was the problem in my case)

This error was giving me hard time on testing my app on BrowserStack. This was the stack trace

app/ios/RunnerUITests/RunnerUITests.m:5: error: -[RunnerUITests example_test Navigation to Booking WebView test] : ((passed) is true) failed - [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() 
 #0      MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:195)
#1      Firebase.app (package:firebase_core/src/firebase.dart:79)
#2      FirebaseRemoteConfig.instance 

We were also doing the Firebase.initializeApp call

await Firebase.initializeApp(
      options: FirebaseOptions(
        apiKey: dotenv.env['FIREBASE_API_KEY']!,
        appId: 'appID',
        projectId: 'my-sample-app',
        iosBundleId: 'com.sample.app.iphone',
      );,
   name: isTesting ? 'my-sample-app' : defaultFirebaseAppName,
    );

As you can see, the name was being overridden to my-sample-app under isTesting flag.

And this was the problem as we were trying to load an instance using FirebaseRemoteConfig.instance

and as a result it was failing as its not a default name.

/// The default Firebase application name.
const String defaultFirebaseAppName = '[DEFAULT]';

and below code checks for the default firebase app to be loaded else throws exception

FirebaseAppPlatform app([String name = defaultFirebaseAppName]) {
    if (appInstances.containsKey(name)) {
      return appInstances[name]!;
    }

    throw noAppExists(name);
  }

So make sure you haven't overriden the name unless you are then calling FirebaseRemoteConfig.instanceFor(app: Firebase.app('my-sample-app') instead of FirebaseRemoteConfig.instance

Lalia answered 19/8 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.