firebase - 无法使用 Flutter Image Network 从 Firebase 存储加载图像
问题描述
我在 Firestore 中创建了虚拟项目列表,并将图像存储在我的存储中。
这是我读取图像表单子项的代码:ListView.separated(
itemBuilder: (context, int index) {
return ListTile(
leading: Image.network(
itemNotifier.itemList[index].imageURL),
width: 100.0,
height: 100.0,
),
title: Text('${itemNotifier.itemList[index].name}'),
subtitle: Text(
'${itemNotifier.itemList[index].description}',
maxLines: 2,
),
trailing: Text(
'${itemNotifier.itemList[index].price.toString()}',
),
);
当我尝试查看此项目列表页面时收到的错误如下:
════════ 图片资源服务捕获到的异常═════════════════════════════␕════ ════════════ 解析图像编解码器时引发以下 NetworkImageLoadException:HTTP 请求失败,状态码:404,http://(db-name).appspot.com/images/pablo-downloading。 PNG
抛出异常时,这是堆栈:#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:95:9) #1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart :48:14) #2 ImageProvider.resolveStreamForKey。(package:flutter/src/painting/image_provider.dart:501:13) #3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:359:22) ... 图片提供者:NetworkImage("http: //(db-name).appspot.com/images/pablo-downloading.png", scale: 1.0) Image key: NetworkImage("http://(db-name).appspot.com/images/pablo-downloading .png",比例:1.0) ═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ════════
我在这里能错过什么?图像 url 的字符串以“ gs:// ”开头是否正常?我每次都必须对读写进行编码和解码吗?
解决方案
您上传到 Firebase 存储的每张图片都会生成两个网址
- 存储地址(以 gs:// 开头)
- 下载地址(以 https:// 开头)
您需要使用下载网址
要在 Flutter 中获取下载 url,您可以参考以下代码块 StorageReference
firebaseStorageRef=FirebaseStorage.instance.ref().child('fileName');
var url = await firebaseStorageRef.getDownloadURL();
要从 firebase 存储 UI 控制台获取下载链接,请单击图像,将出现一个左侧对话框,然后右键单击带下划线的蓝色图像名称,然后单击在新选项卡中打开
推荐阅读
- ios - 以编程方式相对于安全区域的底部定位视图
- java - Android:ListView中每一行的动态复选框数
- django - StreamField 块中的图像无法识别
- python - 全局变量在 Python Tkinter 中充当局部变量
- mysql - 如何在 mySQL 工作台中创建表?
- php - 通过 PHP 以 HTML 格式将视频存储在 MySQL 输出中
- ruby-on-rails - 应用程序不会通过 Sendgrid 发送确认电子邮件并设计
- mongodb - 为图像生成 URL?
- java - 如何将具有不同值的相同对象生成到 ArrayList 中?
- raspberry-pi3 - Raspberry Pi 3 B+ hamachi 主机名问题