Flutter Web 应用程序在部署到 Firebase 托管后返回空白页面

Posted

技术标签:

【中文标题】Flutter Web 应用程序在部署到 Firebase 托管后返回空白页面【英文标题】:Flutter web app is returning blank page after being deployed to firebase hosting 【发布时间】:2021-07-23 01:20:57 【问题描述】:

我已成功将我的 Flutter Web 应用部署到 Firebase 托管。但是当我访问链接空白页时返回 这是我的 firebase.json

    
  "database": 
    "rules": "database.rules.json"
  ,
  "firestore": 
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  ,
  "hosting": 
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  ,
  "storage": 
    "rules": "storage.rules"
  ,
  "emulators": 
    "auth": 
      "port": 9099
    ,
    "functions": 
      "port": 5001
    ,
    "firestore": 
      "port": 8080
    ,
    "database": 
      "port": 9000
    ,
    "hosting": 
      "port": 5000
    ,
    "pubsub": 
      "port": 8085
    ,
    "ui": 
      "enabled": true
    
  ,
  "remoteconfig": 
    "template": "remoteconfig.template.json"
  

这是我的 index.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.

    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="A new soicial network.">

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

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

  <title>LycRead Web</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?v=2330050811');
      );
    
  </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.4.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/8.4.1/firebase-analytics.js"></script>

  <script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-messaging.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-storage.js"></script>

  <script>
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    var firebaseConfig = 
      apiKey: "my config",
      authDomain: "my config",
      projectId: "my config",
      storageBucket: "my config",
      messagingSenderId: "my config",
      appId: "my config",
      measurementId: "my config"
    ;
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
  </script>
</body>

</html>

此外,当我使用 Chrome 打开 index.html 时,它也会返回空白页。但是,当我使用控制台运行项目时,一切正常。

这是我部署后的终端输出

【问题讨论】:

【参考方案1】:

我发现了问题 它在 index.html 中 这部分代码

<script>
    if ('serviceWorker' in navigator) 
      window.addEventListener('flutter-first-frame', function () 
        navigator.serviceWorker.register('flutter_service_worker.js?v=2330050811');
      );
    
  </script>
  <script src="main.dart.js" type="application/javascript"></script>

应该在body标签的末尾

【讨论】:

以上是关于Flutter Web 应用程序在部署到 Firebase 托管后返回空白页面的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:Fire Base 存储图像上传的后期初始化错误

Firebase 托管 Flutter Web 应用程序未清除首次部署的缓存

将 Flutter Web App 部署到 Hostgator/普通 FTP 意味着啥?

如何在 Heroku 上部署 Flutter Web

Flutter web 在部署 web 应用程序时使用 singleChildScrollView 显示空白

如何保护部署到 Firebase Hosting 的 Flutter Web 的源代码?