【问题标题】:No Firebase App has been created - call Firebase.initializeApp()尚未创建任何 Firebase 应用程序 - 调用 Firebase.initializeApp()
【发布时间】:2022-12-30 02:21:26
【问题描述】:

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

[core/no-app] 没有创建 Firebase 应用“[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(),
        },
      ),
    );
  }
}

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

【问题讨论】:

  • Baimam Boukar 的回答对您有帮助吗?
  • 它给出了这个错误“[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.”

标签: firebase flutter


【解决方案1】:

你应该在 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());
}

This link 可能对你有帮助。

编辑:第一个链接已存档。 This is 在 Flutter 项目中设置 firebase 的新方法。步骤都差不多。最后一个链接包含配置 firebase 插件的更多步骤。

步骤是:

【讨论】:

  • 它给出了这个错误:未定义的名称'DefaultFirebaseOptions'。
  • 您好@AdilNaseem,您是否遵循了文档步骤?第一步:dart pub global activate flutterfire_cli,第二步:flutterfire configure这一步将创建一个名为firebase_options.dart的文件,您应该将其放入项目文件并从 main.dart 中调用它。
  • 当我在步骤 -1 中尝试此命令时,出现此错误“'dart' 未被识别为内部或外部命令、可运行程序或批处理文件。”
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    Firebase.initializeApp() 是一个异步函数。您应该使用“await”来确保 Firebase 初始化。并使用 WidgetsFlutterBinding.ensureInitialized() 来防止颤动错误。

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

    【讨论】:

      【解决方案4】:

      问题是你已经在你的 flutter 项目中配置了 firebase 但你还没有在应用程序启动之前初始化它。

      您需要异步初始化 firebase sdk。这就是解决方法。

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

      【讨论】:

      • 它给出了这个错误“[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.”
      【解决方案5】:

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

      【讨论】:

        【解决方案6】:
        1. 检查你的 android/build.gradle 文件。 内部依赖添加

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

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

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

        【讨论】:

          猜你喜欢
          • 2021-08-14
          • 2021-04-10
          • 2021-12-29
          • 1970-01-01
          • 2023-01-20
          • 1970-01-01
          • 2020-12-24
          • 2020-12-12
          相关资源
          最近更新 更多