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

Time:12-27

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related