繁体   English   中英

没有创建 Firebase App - 调用 Firebase.initializeApp()

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

我正在尝试运行我的应用程序,但出现以下错误

[core/no-app] 没有创建 Firebase App '[DEFAULT]' - 调用 Firebase.initializeApp()

我已经调用firebase.initializeApp()但错误仍然相同。 这是我的主文件代码

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

非常感谢任何形式的帮助。 谢谢

您应该在 main() 中对其进行初始化。

例如:

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

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

此链接可能对您有所帮助。

如果上述答案没有帮助,您可以通过以下步骤解决问题:

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

它从字面上解决了我的问题。 我希望这将有所帮助。

Firebase.initializeApp() 是一个异步 function。 您应该使用“等待”来确保 Firebase 初始化。 并使用 WidgetsFlutterBinding.ensureInitialized() 来防止 flutter 错误。

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

问题是您在 flutter 项目中配置了 firebase,但在应用程序启动之前您尚未对其进行初始化。

您需要异步初始化 firebase sdk。 这是如何 go 关于它的。

 Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); }

当您未能在项目和应用程序级别 build.gradle 文件中的 android 方面添加所需的依赖项时,就会发生此错误

  1. 检查你的 android/build.gradle 文件。 内部依赖添加

    类路径 'com.google.gms:google-services:4.3.14'(使用最新版本)

  2. Go 到 app/build.gradle 文件。 添加插件外部依赖

    应用插件:'com.google.gms.google-services'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM