简体   繁体   English

Firebase 未正确初始化

[英]Firebase is not getting initialized right

So, I wanted to include firebase into my Flutter App, it works fine for android but when I start it on my ios emulator it will always give me this error So, I wanted to include firebase into my Flutter App, it works fine for android but when I start it on my ios emulator it will always give me this error

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized. [VERBOSE-2:ui_dart_state.cc(209)] 未处理的异常:[core/not-initialized] Firebase 尚未正确初始化。 Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp .通常这意味着您在调用Firebase.initializeApp之前尝试使用 Firebase 服务。 View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization查看文档以获取更多信息: https://firebase.flutter.dev/docs/overview#initialization

I have the google-service file in the right directory and I followed the tutorial and edited my main() to:我在正确的目录中有 google-service 文件,我按照教程并将我的 main() 编辑为:

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

Has anybody the same problem with IOS or did I do anything wrong?有人与 IOS 有同样的问题,还是我做错了什么?

So for everybody else getting this error I figured it out myself, for IOS, the line因此,对于其他遇到此错误的人,我自己想通了,对于 IOS,行

  Firebase.initializeApp()

it needs an option:它需要一个选项:

Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)

for that follow the instructions on this link:为此,请按照此链接上的说明进行操作:

https://firebase.flutter.dev/docs/cli https://firebase.flutter.dev/docs/cli

hope that helps anyone希望对任何人都有帮助

  1. Install Firebase CLI if it is not installed in your system.如果系统中未安装Firebase CLI ,请安装它。
  2. Next, install the FlutterFire CLI or by running the following command: dart pub global activate flutterfire_cli .接下来,安装FlutterFire CLI或运行以下命令: dart pub global activate flutterfire_cli Once installed, the flutterfire command will be globally available.安装后,flutterfire 命令将全局可用。
  3. Run flutterfire configure in your flutter project folder in terminal,在终端的 flutter 项目文件夹中运行flutterfire configure
  4. Select a Firebase project to configure your Flutter application with, Select 一个 Firebase 项目来配置您的 Flutter 应用程序,
  5. Select/Insert android app id and bundle id you want to use as and when it asks you.选择/插入 android 应用程序 ID 和捆绑 ID,您想在询问时使用。
  6. and get firebase_options.dart.并获取 firebase_options.dart。
  7. In main.dart file import firebase_options.dart.在main.dart文件中导入firebase_options.dart。
  8. In void main() use following code.在 void main() 中使用以下代码。

        WidgetsFlutterBinding.ensureInitialized();
         if (Platform.isIOS) {
         await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform,
         );
           } else {
             await Firebase.initializeApp();
             }
        runApp(
        const MaterialApp(
        home: MyApp(),
        ),
      );

I got same problem.我遇到了同样的问题。 My Android application is working fine but when using IOS application it's throw this type of exception.我的 Android 应用程序运行良好,但在使用 IOS 应用程序时会抛出此类异常。 you can use @Viki's answer and if its not working for you make sure you have added GoogleServices-info.plist file in your ios project.您可以使用@Viki 的答案,如果它不适合您,请确保您在 ios 项目中添加了GoogleServices-info.plist文件。 If it's not solve your issue then use this code如果它不能解决您的问题,请使用此代码

if (Platform.isIOS) {
    await Firebase.initializeApp(
        options: FirebaseOptions(
            apiKey: "your api key Found in GoogleService-info.plist",
            appId: "Your app id found in Firebase",
            messagingSenderId: "Your Sender id found in Firebase",
            projectId: "Your Project id found in Firebase"));
  } else {
    await Firebase.initializeApp();
  }

In my case, I haven't added the code lines according to the instructions from firebase.就我而言,我没有根据 firebase 的说明添加代码行。 The simple fix is to follow the instructions from the firebase doc from start to finish.简单的解决方法是从头到尾遵循 firebase 文档中的说明。

在此处输入图像描述

I have faced the same issue in my flutter application when run on xcode/ ios.在 xcode/ios 上运行时,我在 flutter 应用程序中遇到了同样的问题。 Here is how I solved.这是我解决的方法。

  1. Download GoogleService-Info.plist from firebase account从 firebase 帐号下载 GoogleService-Info.plist
  2. Add Files to Runner like below and choose GoogleService-Info.plist from download folder像下面这样将文件添加到 Runner 并从下载文件夹中选择 GoogleService-Info.plist

在此处输入图像描述

  1. Re-Run the project, It will work重新运行项目,它会工作

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

相关问题 没有 Firebase 应用程序“[默认]”错误,但 firebase 已经初始化 - No Firebase App '[Default]' error but firebase is already initialized Flutter: Firebase 尚未正确初始化 - Flutter: Firebase has not been correctly initialized 检查 Firebase 应用程序是否已在 python 中初始化 - check if a Firebase App is already initialized in python 如何检查 Firebase 应用程序是否已在 Android 上初始化 - How to check if a Firebase App is already initialized on Android 努力重新获得基础以识别我初始化的 firebase DB - Struggling to get re-base to recognise my initialized firebase DB 正确的数据未到达 Twilio - The right data is not getting to Twilio 初始化 Firebase 后出现“无默认应用”异常 - 'no default app' exception after Firebase has been initialized 初始化 firebase 时,Flutter 代码不运行 - Flutter code doesn't run when firebase is initialized Ionic Capacitor firebase 推送通知,报错:Default FirebaseApp is not initialized in this process - Ionic Capacitor firebase push notification, error:Default FirebaseApp is not initialized in this process 这是在 Firebase 应用程序中跟踪按钮的正确代码吗 - Is this the right code for tracking a button in a an app for Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM