Flutter/FireStore:如何在Flutter中显示Firestore中的图像? [英] Flutter / FireStore: how to display an image from Firestore in Flutter?
问题描述
我想将我在应用程序中使用的某些图像放到Firestore中,然后从那里显示它们,而不是将它们作为资产捆绑在我的应用程序中.
I want to put some images that I use in my app to Firestore, and display them from there, rather than having them as assets, bundled in my app.
为此,我想出了以下解决方案:对于要显示图像的项目,我创建了一个Firebase文档,其中有一个字段,用于存储存储相应图片的文件的名称. :
In order to do this, I came up with the following solution: for the item I want to display an image, I created a Firebase document, where I have a field that stores the name of the file that stores the corresponding picture:
document
- name
- description
- imageName
然后,我以相同的名称将图像上传到FireStore.
Then, I uploaded an image to FireStore with the same name.
当我要显示图像时,可以通过StreamBuilder成功获取文档,并提取imageName属性.
When I want to display the image, I can successfully get the document via StreamBuilder, and extract the imageName property.
我还创建了必要的FireStore引用:
I also created the necessary FireStore references:
FirebaseStorage storage = new FirebaseStorage(
storageBucket: 'gs://osszefogasaszanhuzokert.appspot.com/'
);
StorageReference imageLink = storage.ref().child('giftShopItems').child(documentSnapshot['imageName']);
但是,我似乎无法将其传递给Image.network()
小部件.
However, I can't seem to pass it to an Image.network()
widget.
是否可以按照我想要的方式显示图像,还是应该使用其他图像提供程序?
Is it possible to display an image the way I'd like to, or should I use a different Image provider?
推荐答案
您需要StorageReference imageLink
中的.getDownloadURL()
来获取存储URL链接:
You need the .getDownloadURL()
from the StorageReference imageLink
to get the storage URL link:
final imageUrl = await imageLink.getDownloadUrl();
Image.network(imageUrl.toString());
这篇关于Flutter/FireStore:如何在Flutter中显示Firestore中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!