Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 660
Flutter Beginner Avatar asked Dec 26 '21 13:12

Flutter Beginner


People also ask

What does initializeApp do in Firebase?

initializeApp. Creates and initializes a Firebase app instance. See Add Firebase to your app and Initialize multiple projects for detailed documentation.

How do I initialize Firebase app in react native?

Creating and configuring a firebase projectHead over to Firebase console and add a new Firebase project, name it React Native Todo App . Once the project is set and ready, click Continue. You will be redirected to the console of that newly created project. React Native is cross-platform.

What is Firebase core?

The firebase_core plugin is responsible for connecting your Flutter app to your Firebase project. The plugin must be installed and initialized before the usage of any other FlutterFire plugins. It provides basic functionality such as: Initializing FlutterFire. Creating Secondary Firebase App Instances.

How to initialize default app in Firebase?

Default app is initialized after the other one. Correct way is to initialize the default app first and then the rest. firebase.apps.app () is being called before default app initialization. This code is basically returning the default app instance. Since it is not present, hence the error.

How to solve “no Firebase app [default] has been created” error in flutter?

In this way, you can solve "No Firebase App ' [DEFAULT]' has been created - call Firebase.initializeApp ()" error in the Flutter app.

What is the difference between default Firebase and custom Firebase?

Why this mattered for you is likely that code can implicitly use the default by eg. firebase.firestore () whereas with the custom "app" (first) the return value, an "app handle" must be passed around. I use both ways. The default for the main web app and the named variant within a library.

Why is Firebase Cloud Messaging not working?

This error is caused when you use any Firebase services such as Cloud Messaging, Firestore without initializing Firebase core. Here, in our case, this error occurs while using Firebase cloud messaging. The cause of this error is you might not have initialized firebase before using firebase service, or you may have initialized like below:


2 Answers

You should initialise it in main().

For example:

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

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

This link might be helpful for you.

like image 66
Eray Avatar answered Oct 09 '22 18:10

Eray


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.

like image 43
Adil Naseem Avatar answered Oct 09 '22 18:10

Adil Naseem