firebase - Flutter:未捕获的 ReferenceError:firebase 未定义

标签 firebase flutter

我在 Flutter Web 应用程序中使用 Firebase 服务并且在加载 firebase 脚本时遇到问题。

当我使用 flutter run -d chrome 时,我看到空白页面有错误,并且在重新加载页面后加载正常:-/。

当我执行 flutter build web 时 - 页面从未加载,重新加载无济于事。

基本错误:
未捕获的 ReferenceError:firebase 未定义

这是我在第一次启动时看到的 flutter run -d chrome :

Uncaught ReferenceError: firebase is not defined
    at (index):26
app.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
require.js:143 Uncaught Error: Script error for "@firebase/app", needed by: dart_sdk
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js:168)
    at HTMLScriptElement.onScriptError (require.js:1738)
:57663/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)

当我做 flutter build web - 只有这个错误:
Uncaught ReferenceError: firebase is not defined
    at Object.aoq (top_level.dart:80)
    at Object.auD (cloud_firestore_web.dart:33)
    at cloud_firestore_web.dart:26
    at aom.a (async_patch.dart:315)
    at aom.$2 (async_patch.dart:340)
    at Object.G (async_patch.dart:245)
    at Object.UR (main.dart:8)
    at js_helper.dart:3246
    at js_helper.dart:3246
    at dartProgram (js_helper.dart:3246)

我的 index.html 文件:
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>new_flowers</title>
</head>
<body>
  <script src="main.dart.js" type="application/javascript"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-app.js"></script>
  <script src ="https://www.gstatic.com/firebasejs/7.15.0/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-auth.js"></script>
  <script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "...",
    measurementId: "..."
  };

  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  </script>
</body>
</html>

最佳答案

你的 index.html 有点乱。尝试像这样定义你的脚本:

  • Firebase 应用 + 使用的 CDN
  • Firebase 应用配置
  • 服务 worker
  • Main.dart.js

  • 编辑:代码格式不正确,这里是 html 代码的链接:`
    <head>
      <meta charset="UTF-8">
      <meta content="IE=Edge" http-equiv="X-UA-Compatible">
      <meta name="description" content="A new Flutter project.">
    
      <!-- iOS meta tags & icons -->
      <meta name="apple-mobile-web-app-capable" content="yes">
      <meta name="apple-mobile-web-app-status-bar-style" content="black">
      <meta name="apple-mobile-web-app-title" content="bevent">
      <link rel="apple-touch-icon" href="icons/Icon-192.png">
    
      <!-- Favicon -->
      <link rel="shortcut icon" type="image/png" href="favicon.png" />
    
      <title>BEvent</title>
      <link rel="manifest" href="manifest.json">
    </head>
    
    <body>
      <!-- The core Firebase JS SDK is always required and must be listed first -->
      <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-app.js"></script>
    
      <!-- TODO: Add SDKs for Firebase products that you want to use
         https://firebase.google.com/docs/web/setup#available-libraries -->
      <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-auth.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-firestore.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.15.2/firebase-storage.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-analytics.js"></script>
    
      <script>
        // Your web app's Firebase configuration
        var firebaseConfig = {
          ...
        };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        firebase.analytics();
      </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>
        if ('serviceWorker' in navigator) {
          window.addEventListener('load', function () {
            navigator.serviceWorker.register('flutter_service_worker.js');
          });
        }
      </script>
      <script src="main.dart.js" type="application/javascript"></script>
    </body>
    
    </html>`
    

    关于firebase - Flutter:未捕获的 ReferenceError:firebase 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62448659/

    相关文章:

    dart - Flutter-无法从 StreamBuilder 加载 Widget

    php - 无法将视频从flutter上传到服务器php?

    python - 在字符串中查找和格式化非正式日期的正确方法是什么?

    ios - 当应用程序在后台运行或关闭时,Flutter Firebase Messaging 在 IOS 上不起作用

    ios - Firebase 无法在 iOS 上运行

    android - 将 firebase 身份验证与谷歌应用引擎云端点集成

    flutter - 当我运行 (F5) Flutter Web 应用程序时,chrome 上出现空白屏幕(我使用 VScode)

    flutter - Flutter StatefulWidget不更新数据

    firebase - 如何使用Flutter在Firestore中显示图像文件

    firebase - 哪个字段用于在 Firestore 仪表板中按 ID 过滤文档?