Flutter 从 Firebase 存储加载图像

2024-01-11

我看到有很多关于如何使用 flutter 将图像上传到 firebase 存储的示例,但没有实际下载/读取/显示已上传的图像。

在Android中,我只是使用Glide要显示图像,我该如何在 Flutter 中执行此操作?我是否使用NetworkImage类,如果是这样,我如何首先获取存储在 Storage 中的图像的 url?


要查看存储中的图像,您需要的是存储中文件的名称。获得所需特定图像的文件名后。 就我而言,如果我想加载测试图像,

final ref = FirebaseStorage.instance.ref().child('testimage');
// no need of the file extension, the name will do fine.
var url = await ref.getDownloadURL();
print(url);

获得网址后,

Image.network(url);

就这样 :)

基于其中一条评论的新替代答案。

我没有看到谷歌在任何地方对 downloadURL 收取额外费用。 因此,如果您要发表一些评论,请附上链接。

将文件上传到存储后,使该文件名唯一并将该名称保存在 firestore 或实时数据库中的某个位置。

getAvatarUrlForProfile(String imageFileName) async {
final FirebaseStorage firebaseStorage = FirebaseStorage(
    app: Firestore.instance.app,
    storageBucket: 'gs://your-firebase-app-url.com');

Uint8List imageBytes;
await firebaseStorage
    .ref()
    .child(imageFileName)
    .getData(100000000)
    .then((value) => {imageBytes = value})
    .catchError((error) => {});
return imageBytes;
}

Uint8List avatarBytes =
    await FirebaseServices().getAvatarUrlForProfile(userId);

并像这样使用它,

MemoryImage(avatarBytes)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter 从 Firebase 存储加载图像 的相关文章

随机推荐