尚未创建 Firebase 应用程序 - 调用 Firebase.initializeApp()

Stu*_*ent 11 firebase flutter

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

[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(),
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Era*_*ray 14

您应该在 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());
}
Run Code Online (Sandbox Code Playgroud)

此链接可能对您有帮助。

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

步骤是:


Adi*_*eem 4

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

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
Run Code Online (Sandbox Code Playgroud)

它确实解决了我的问题。我希望这个能帮上忙。