geting_flutter在flutter web中未定义,仅在生产中定义

ttp71kqs  于 2022-12-14  发布在  Flutter
关注(0)|答案(5)|浏览(195)

我有一个错误,一直出现在我的最后几个部署,我无法跟踪它。它只发生在我部署到firebase网站,调试工作没有任何问题。

Uncaught SyntaxError: Unexpected token '<' (at flutter.js:1:1)
(index):50 Uncaught ReferenceError: _flutter is not defined
    at (index):50:7

这会显示在浏览器控制台中。
这是我的索引.html:

<!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.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

    This is a placeholder for base href that will be replaced by the value of
    the `--base-href` argument provided to `flutter build`.
  -->
  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="Trading made easy">
  <meta name="google-signin-client_id"
    content="xxx.apps.googleusercontent.com">

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

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

  <title>Torn Market</title>
  <link rel="manifest" href="manifest.json">
  <!-- <script>self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;</script> -->
  <script>
    // The value below is injected by flutter build, do not touch.
    var serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>

<body>
  <script>
    window.addEventListener('load', function (ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function (engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function (appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>

</html>
vcudknz3

vcudknz31#

打开 your_project_name/build/web 路径中的index.html文件,并将以下脚本放在body标记之后:<script src="main.dart.js" type="application/javascript"></script>。如果它没有工作,那么可能flutter.js从构建目录中丢失,所以在终端中通过flutter clean清理您的项目,然后使用flutter build web再次构建它,然后添加我上面提到的标记。现在将您的web部署到主机上。

ego6inou

ego6inou2#

这里有一个解决方案对我很有效。
在index.html中

更改此项

<base href="/">

"敬这个"

<base href="./">
hmmo2u0o

hmmo2u0o3#

您很可能缺少build/web文件夹中的flutter.js文件

l7wslrjt

l7wslrjt4#

我不知道下面的代码是否有效。请尝试实现相同的代码并恢复请按如下所示更改body标记

<body>
<script src="main.dart.js" type="application/javascript"></script>
  <script>
  if ('serviceWorker' in navigator) {
      window.addEventListener('flutter-first-frame', function () {
        navigator.serviceWorker.register('flutter_service_worker.js');
        navigator.serviceWorker.register('flutter_service_worker.js');
      });
    }
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        }
      }).then(function(engineInitializer) {
        return engineInitializer.initializeEngine();
      }).then(function(appRunner) {
        return appRunner.runApp();
      });
    });
  </script>
</body>
x0fgdtte

x0fgdtte5#

您应该尝试使用以下命令重新生成应用程序:

flutter clean

然后再

flutter build web

我尝试手动删除build文件夹,但没有工作。最好使用flutter clean

相关问题