问题
我有一个使用CanvasKit渲染的Flutter Web项目,托管在Firebase Hosting上,但我无法加载托管在Xano CDN上的外部图像或加载GooglePlaces API结果(自动编译)。 我阅读了许多其他解决方案(例如这个,这个或这个),但都无效。 我还配置了 Google Cloud 上的CORS。
我的文件
这是我的firebase.json
文件:
{
"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "*",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}
]
}
}
在index.html文件中,我在
</body>
标签之前添加了以下脚本:<!-- Firebase -->
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-analytics.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: *mykey*,
authDomain: "hearth-148b0.firebaseapp.com",
projectId: "hearth-148b0",
storageBucket: "hearth-148b0.appspot.com",
messagingSenderId: *id*,
appId: *myappid*,
measurementId: *id*
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
</script>
我试图将头文件包含在CachedNetworkImage中:
return CachedNetworkImage(
httpHeaders: {"Access-Control-Allow-Origin": "*"},
imageUrl: imageUrl,
fit: fit,
width: width,
height: height,
progressIndicatorBuilder: (context, url, downloadProgress) =>
LoadingPlaceholder(width: width),
errorWidget: (context, url, error) => SizedBox(width: width),
);
我尝试将标题包含在GooglePlace初始化器中:
final GooglePlace googlePlace = GooglePlace(
kTokenGMaps,
headers: {"Access-Control-Allow-Origin": "*"},
);
错误
这是浏览器控制台显示的错误类型:
无法加载资源:net::ERR_BLOCKED_BY_CLIENT
或者
访问XMLHttpRequest位于 'https://storage.googleapis.com/xatr-ly1x-gkt1.n7.xano.io/vault/xhL5_hu1/fMPW0YCx/C01_Ng../9de15d31/Monumento_ai_caduti_in_Corso_Europa.jpg' (重定向自 'https://xatr-ly1x-gkt1.n7.xano.io/vault/xhL5_hu1/fMPW0YCx/C01_Ng../Monumento_ai_caduti_in_Corso_Europa.jpg?tpl=big:box') 源' https://hearth-148b0.web.app '的CORS策略已阻止对请求的资源进行访问:在所请求的资源上不存在“Access-Control-Allow-Origin”标头。
部署
这些是我从Android Studio使用的用于在Firebase上部署的命令:
flutter build web
firebase deploy
备选方案
我还尝试使用另一个主机和FTP,并将.htaccess文件设置为:
Header set Access-Control-Allow-Origin *
但是没有成功。