相同的Flutter Web构建在本地使用dhttp运行,但在使用firebase serve或firebase deploy时失败。

时间:2019-12-15 09:58:19

标签: firebase flutter google-cloud-firestore firebase-hosting flutter-web

我正在为移动和网络开发Flutter应用程序。它使用了多个软件包,包括用于Firestore的软件包:

  cloud_firestore: ^0.12.9+1 #imported conditionally for mobile
  firebase_web: ^5.0.9 #imported conditionally for web
  #firebase_core: ^0.4.3+1 #not sure whether it's required in my case, assuming not (?)

我的web/index.html包含以下脚本:

  <script src="main.dart.js" type="application/javascript"></script>
  <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-firestore.js"></script>
  <script src="/__/firebase/init.js"></script>

1)该应用在调试模式下可以正常工作。

2)然后,我使用flutter build web进行构建,其结果文件结构类似于the tutorial中的文件结构。

2.1)当我使用dhttpd flutter pub global run dhttpd --path build/web

在本地运行它时,它可以按预期工作

3)然后,我尝试将其部署到Firebase托管。

运行firebase init时,我将build/web设置为公共目录。起初,我对this question的答案感到困惑,因为它应该只是build,但我认为对于Flutter for web的预览版来说是正确的,现在是build/web,其中{{ 1}}生活。

关于配置为单页应用(将所有网址重写为/index.html),我尝试了是/否,没有明显的区别。再次,从注释到上方链接的答案有一个小混乱:“您的另一个错误是将其配置为单页应用程序。它将找不到您可能拥有的任何资产。”

3.1)首先,在the tutorial index.htmlfirebase serve之后在本地进行尝试-它们都给了我一个空页面,其背景与所定义主题的颜色正确,但是没有其他东西了。在页面检查器和“源”选项卡中,我看到所有与步骤(2.1)相同的html结构和脚本,唯一的区别是在Firebase的情况下,额外的目录firebase serve --only hostingassetes

firebase serve local results

3.2)尝试__/firebase会产生与(3.1)中相同的空白背景屏幕。 firebase deploy中没有错误可供调查。我还应该查看其他哪些日志?

(使用Flutter最新版本主版v1.13.3-pre.12,Firebase工具v7.10.0)

2 个答案:

答案 0 :(得分:1)

尝试修改您的web/index.html

来自

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

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

这样,当页面解析完成后,便会执行“ main.dart.js”脚本

答案 1 :(得分:0)

因此,问题出在将脚本包含在web/index.html中的顺序。将/__/firebase/init.js放在首位即可解决:

  <script src="/__/firebase/init.js"></script>
  <script src="main.dart.js" type="application/javascript"></script>
  <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-firestore.js"></script>

感谢@ emanuel-muroni将我引向答案。