firebase - Flutter Web 应用程序缺少 firebase.json 并在部署到 Firebase 托管时显示“欢迎 Firebase 托管设置完成”
问题描述
背景
- 我正在使用 Android Studio 创建一个 Flutter Web 应用程序。
- 该应用程序在 Google Chrome 的本地主机上成功运行。
- 当我将其部署到 Firebase 托管时,会显示“Firebase 托管设置完成”页面而不是应用程序。
我试过 的有几个 Stack Overflow 帖子没有解决这个问题。这些包括:
- 在项目的根目录中创建一个 firebase.json 文件。
- 等待主机上线
- 确保我们
build/web
在运行 firebase init 命令时使用 - 在 InCognito 模式下打开托管 URL
- 确保在运行 firebase init 时不会覆盖 index.html
- 确保将正确的脚本粘贴到 index.html 文件中
遵循的步骤
- 在 Android Studio 中,创建一个新的 Flutter 应用。
- 我使用包名
au.com.mydomain
(mydomain
我拥有的域名在哪里)。 - 我注意到项目文件结构中没有 Web 文件夹。
- 我打开一个终端窗口并导航到应用程序的根文件夹。
- 我运行命令
flutter create web
。 - 该命令导致“全部完成!” 和“为了运行你的应用程序,输入
cd web
和flutter run
”。 - 我执行命令
cd web
和flutter run
. 该应用程序在 Google Chrome 的本地主机上成功运行。 - 我返回 Android Studio 并注意到项目文件结构中仍然没有 Web 文件夹。
- 我使用我的网络浏览器导航到 Firebase 并创建一个我称之为的新项目
demo159752
。 - 我为此项目禁用了 Google Analytics。
- 项目创建成功。
- 在 Firebase 的 demo159752 项目中,我添加了一个 Web 应用程序。
- 我
demo
用作应用程序昵称。 - 我没有选中“同时设置 Firebase 托管...”复选框。
- 我单击注册应用程序按钮。
- 我阅读了说明我应该“将这些脚本复制并粘贴到您的标签底部,但在您使用任何 Firebase 服务之前”的说明
- 我返回 Android Studio 执行此操作,并注意到
web
现在在 Android Studio 的文件结构中可以看到一个文件夹。 - 我打开这个
web
文件夹。它不包含 index.html 文件,但包含一个子文件夹,也称为web
. - 我打开
web
子文件夹,发现它包含一个 index.html 文件。 - 我打开 index.html 文件并导航到
</body>
. 我从上面的 firebase 粘贴代码</body>
。 - 我在 Web 浏览器中返回 Firebase,然后单击 Continue to Console。
- 在 Firebase 控制台中,我单击托管。
- 我单击“开始”并被提升为运行
npm install -g firebase-tools
以安装 Firebase CLI。它已经安装在我的机器上,但我还是从终端运行这个命令。这导致语句“更新了 1 个包”。 - 我在网络浏览器中返回 Firebase。我没有单击名为“同时向我展示将 Firebase JavaScript SDK 添加到我的网络应用程序的步骤”选项并继续执行第 2 步。
- 我被指示运行
firebase login
我所做的命令。这会导致声明:“已经登录为emailAddress
”(emailAddress
我的电子邮件地址在哪里)。 - 我返回浏览器并被指示运行命令
firebase init
,我这样做了。然后我导航到 Hosting 选项,通过按键盘上的空格键选择它,然后按 Enter。 - 有人问我要使用哪个 firebase 项目,然后我选择了
demo159752
. - 我使用以下选项:
- 你想用什么作为你的公共目录?
build/web
. - 配置为单页应用?
Y
. - 使用 GitHub 设置自动构建和部署?
N
. - 我注意到声明:
✔ Wrote build/web/index.html
- 我注意到声明:
i Writing configuration info to firebase.json...
- 我注意到声明:
i Writing project information to .firebaserc...
- 我注意到声明:
✔ Firebase initialization complete!
- 我在网络浏览器中返回 Firebase,然后单击下一步继续执行第 3 步。
- 我被指示运行命令
firebase deploy
。 - 我返回终端窗口并运行此命令。
- 我注意到声明:
✔ Deploy complete! and am gien a hosting URL.
- 我导航到托管 URL 并希望看到我的应用程序。但是,我只看到一个页面显示“Firebase 托管设置完成”。
- 我返回 Android Studio 并检查
firebase.json
文件是否已按预期创建。然而,事实并非如此。 - 我试图通过手动创建
firebase.json
文件来解决这个问题。为此,我在根目录中创建了一个名为firebase.json
并使用以下代码的新文件:
{“托管”:{“公共”:“构建/网络”,“忽略”:[“firebase.json”,“ /.*”,“ /node_modules/**”] } }
- 我返回终端并导航到项目的根目录,然后进入 web 子文件夹,然后运行
flutter build web --release
. - 然后我运行
firebase deploy
。 - 该应用仍未部署,Firebase 托管设置完成页面仍然存在。
问题 为什么我的应用没有成功部署?我做错了什么,我需要做什么才能成功部署它?
其他细节 Flutter 版本:
Flutter 2.1.0-12.1.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 8264cb3e8a (5 weeks ago) • 2021-03-10 12:37:57 -0800
Engine • revision 711ab3fda0
Tools • Dart 2.13.0 (build 2.13.0-116.0.dev)
扑医生
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel dev, 2.1.0-12.1.pre, on Mac OS X 10.15.7 19H524 darwin-x64,
locale en-AU)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for
more details.
[!] Xcode - develop for iOS and macOS
✗ Xcode 11.4.1 out of date (12.0.1 is recommended).
Download the latest version or update via the Mac App Store.
! CocoaPods 1.9.3 out of date (1.10.0 is recommended).
CocoaPods is used to retrieve the iOS and macOS platform side's plugin
code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To upgrade see
https://guides.cocoapods.org/using/getting-started.html#installation for
instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device (1 available)
! Doctor found issues in 2 categories.
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.
For 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 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="web">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<title>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>
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing ?? reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.3.3/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>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCsPqvAN2gvlyCv3J7H1lTscXxB0T5itDE",
authDomain: "demo159752.firebaseapp.com",
projectId: "demo159752",
storageBucket: "demo159752.appspot.com",
messagingSenderId: "222592650517",
appId: "1:222592650517:web:ff8984d70b66dfc1fa2d1c"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
</body>
</html>
firebase.json 文件
{
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
解决方案
我使用这篇文章来帮助我解决问题,并概述了以下步骤:
有一个文件夹,flutter项目根目录下build/web
也有一个文件夹。web
运行firebase init
并选择build/web
何时询问主目录。(不是web
项目根目录下的文件夹)
然后运行flutter build web
, build/web 中的网页文件将被修改。
然后运行firebase deploy --only hosting
,几分钟后网页应该会显示出来。
对我来说,第一次展示需要几个小时,但从第二次开始,我对网站所做的更改不到一分钟就出现了。
推荐阅读
- php - 如何将 2 个 JSON 文件合并为一个 JSON FeatureCollection
- java - 无法从 JAR 执行 Spring JDBC 项目
- java - 如何在arraylist的前n个值上申请循环
- javascript - JavaScript&HTML 中的简单数学游戏,从 if/else 获取错误
- c# - C# 接口隐式提升类似于拆箱,使用已知的接口分层模式:
- mongodb - 如何获得 Post 中所有评论的总长度(计数)?
- sql - 通过在数组数组中搜索/比较返回聚合数组
- mysql - Laravel 一张表通过多对多关系连接到另外两张表
- c - 无效的旧大小重新分配错误,标准输入读取
- python - 如何为 4d 张量中的 k 个最大元素创建一个单热张量?