Flutter web Firebase初始化错误

yi0zb3m4  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(134)

当我启动flutter Web应用程序时,我收到以下错误:
[core/not-initialized] Firebase尚未正确初始化。通常这意味着您在调用Firebase.initializeApp之前尝试使用Firebase服务。

<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
  <script type="module">
    // Import the functions you need from the SDKs you need
    import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.0/firebase-app.js";
    import { auth } from 'https://www.gstatic.com/firebasejs/9.9.0/firebase-auth.js'
    import { firestore } from 'https://www.gstatic.com/firebasejs/9.9.0/firebase-firestore.js'  
    // https://firebase.google.com/docs/web/setup#available-libraries
  
    // Your web app's Firebase configuration
    const firebaseConfig = {
      apiKey: "xxx",
      authDomain: "xxx",
      projectId: "xxx",
      storageBucket: "xxx",
      messagingSenderId: "xxx",
      appId: "xxx"
    };
  
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
  </script>  
  <!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information, see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->
  <script>
         ...
  </script>
</body>
</html>

正如你所看到的,firebase在index.html文件中初始化,我已经安装了所有需要的库,这是列表:

  • 防火墙身份验证:第3.4.2节
  • 云_消防商店:^3.3.0版本
  • Firebase _核心:1919年2月

我应该在Flutter模式下启动火力基地吗?

olhwl3o2

olhwl3o21#

if (kIsWeb) 
    await Firebase.initializeApp(
      options: FirebaseOptions(
        apiKey: "xxx",
        appId: "xxx",
        messagingSenderId: "xxx",
        projectId: "xxx",
      ),
    );
else
   await Firebase.initializeApp();

将上述代码添加到主.dart文件中。(kIsWeb将检查应用程序是否在Web上运行)

相关问题