Flutter web 无法从另一个域加载网络图像 [英] Flutter web can't load network image from another domain

查看:50
本文介绍了Flutter web 无法从另一个域加载网络图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过 API 调用从其他域加载 flutter web 中的网络图像.收到这个错误

<块引用>

试图从另一个域加载图像?在以下位置找到答案:https://flutter.dev/docs/development/platform-integration/web-imagesImageCodecException: 无法加载网络图像.

有什么帮助吗?

解决方案

为了能够在 Flutter 网页上显示来自任何其他域或 Firebase Storage 的图像,您必须为 CORS.

  1. 打开 GCP 控制台,选择您的项目并通过点击启动云终端会话顶部导航栏中的 >_ 图标按钮.

  2. 单击打开编辑器按钮(铅笔图标),然后创建 cors.json 文件.

cors.json 文件应如下所示:

<预><代码>[{起源":[*"],方法":[GET"],maxAgeSeconds":3600}]

我将原点设置为 * 这意味着每个网站都可以显示您的图像.但是您也可以在那里插入您网站的域来限制访问.

  1. 运行 gsutil cors set cors.json gs://your-bucket

如果您需要更多信息:https://cloud.google.com/storage/文档/配置-cors

I can't load network images in flutter web from other domains with API calls. getting this error

Trying to load an image from another domain? Find answers at: https://flutter.dev/docs/development/platform-integration/web-images ImageCodecException: Failed to load network image.

any help?

解决方案

For being able to display your images from any other Domain or from Firebase Storage on a Flutter web page you have to configure your data for CORS.

  1. Open the GCP console, select your project and start a cloud terminal session by clicking the >_ icon button in the top navbar.

  2. Click the open editor button (pencil icon), then create the cors.json file.

The cors.json file should look like this:

[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

I set the origin to * which means that every website can display your images. But you can also insert the domain of your website there to restrict access.

  1. Run gsutil cors set cors.json gs://your-bucket

If you need more information: https://cloud.google.com/storage/docs/configuring-cors

这篇关于Flutter web 无法从另一个域加载网络图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆