【问题标题】:Running flutter web app locally without android studio throws firebase error在没有 android studio 的情况下在本地运行 flutter web 应用程序会引发 firebase 错误
【发布时间】:2021-03-10 12:37:48
【问题描述】:

在本地服务器上成功安装我的 Flutter Web 应用程序后(使用 express 跟随 this answer),我可以在 localhost 上运行我的 Flutter 应用程序。但是,在访问它时,控制台会显示错误

main.dart.js:28998 Uncaught ReferenceError: firebase is not defined

我认为我需要在我的app.js 中安装和引用 firebase,但我实际上不知道它是如何完成的(如果确实如此的话)。我尝试运行命令 npm install firebase 似乎已成功安装它,但我仍然收到此错误。

您知道我可能遗漏了什么或为什么会收到此错误以及如何解决它吗?

请注意,如果在使用 Android Studio 构建时在 Chrome 上运行该项目可以完美运行,因此它必须与本地托管有关。


编辑

这是我当前的 index.html,当我在 Chrome 上通过 Android Studio 运行项目时,它运行良好

<!DOCTYPE html>
<html>
<head>
  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    Fore more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
  -->
  <base href="/">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="App test">

  <!-- 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="my_app">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>My App</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <!-- 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('flutter-first-frame', function () {
        navigator.serviceWorker.register('flutter_service_worker.js');
      });
    }
  </script>

  <script src="https://maps.googleapis.com/maps/api/js?key=MyGoogleMapsKey"></script>

  <script src="main.dart.js" type="application/javascript"></script>

  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/8.1.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.5.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.5.1/firebase-firestore.js"></script>

  <script>
    <!--  Your web app's Firebase configuration-->
    var firebaseConfig = {
      apiKey: "apiKey",
      authDomain: "my-app.firebaseapp.com",
      databaseURL: "https://myApp.firebaseio.com",
      projectId: "my-app",
      storageBucket: "my-app.appspot.com",
      messagingSenderId: "2243255670923",
      appId: "1:3134567819932:web:f09e90ce142l9ikk0d2f27"
    };
    <!--  Initialize Firebase-->
    firebase.initializeApp(firebaseConfig);
  </script>

</body>
</html>

【问题讨论】:

    标签: firebase express flutter dart flutter-web


    【解决方案1】:

    确保在 web 文件夹中的 index.html 中包含整个 firebase 脚本和您正在使用的服务。 之前做 &lt;script src="main.dart.js" type="application/javascript"&gt;&lt;/script&gt; 身体线条。

    请注意,即使 Flutter 与您拥有的 index.html 配合得很好,这也可能是它无法在您的本地服务器上工作的原因。

    在您的情况下,将所有与 firebase 相关的脚本(包括配置脚本)移到 main.dart.js 脚本之前:

    <body>
      ...
      <script src="https://www.gstatic.com/firebasejs/7.5.1/firebase-app.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.5.1/firebase-auth.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.5.1/firebase-firestore.js"></script>
    
      <script>
        var firebaseConfig = {
          apiKey: "apiKey",
          authDomain: "my-app.firebaseapp.com",
          databaseURL: "https://myApp.firebaseio.com",
          projectId: "my-app",
          storageBucket: "my-app.appspot.com",
          messagingSenderId: "2243255670923",
          appId: "1:3134567819932:web:f09e90ce142l9ikk0d2f27"
        };
        
        firebase.initializeApp(firebaseConfig);
      </script>
    
      <script src="main.dart.js" type="application/javascript"></script>
    </body>
    

    【讨论】:

    • 您好,感谢您的回答。不幸的是,这不是问题。我在你提到的地方包含了这些脚本,但我仍然遇到同样的错误。我不明白为什么无论如何我都需要包含它们,因为我最初没有它们,并且正如我所说,如果我通过 Chrome 上的 Android Studio 运行该项目,该项目将完美运行。如果您能想到其他可能缺少的内容,请告诉我
    • @RedDeath 你需要分享你的代码。如果不这样做,任何人都无法有效地帮助您。甚至这个答案都是猜测。分享您的 index.html 和任何相关的 Firebase 初始化代码。
    • 完全正确 - 我不认为这是必要的,但公平点。用我的 index.html 文件更新问题
    • 再次,请注意 index.html 在从 cmd 运行 flutter run 时运行良好
    • 正如答案中所指出的,您是否在 main.dart.js 脚​​本之前移动了 firebase 脚本?
    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多