[英]Flutter / FireStore: how to display an image from Firestore in Flutter?
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. 我想将我在应用程序中使用的某些图像放到Firestore中,然后从那里显示它们,而不是将它们作为资产捆绑在我的应用程序中。
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: 为此,我想出了以下解决方案:对于要显示图像的项目,我创建了一个Firebase文档,其中有一个字段,用于存储存储相应图片的文件的名称:
document
- name
- description
- imageName
Then, I uploaded an image to FireStore with the same name. 然后,我以相同的名称将图像上传到FireStore。
When I want to display the image, I can successfully get the document via StreamBuilder, and extract the imageName property. 当我想显示图像时,我可以通过StreamBuilder成功获取文档,并提取imageName属性。
I also created the necessary FireStore references: 我还创建了必要的FireStore参考:
FirebaseStorage storage = new FirebaseStorage(
storageBucket: 'gs://osszefogasaszanhuzokert.appspot.com/'
);
StorageReference imageLink = storage.ref().child('giftShopItems').child(documentSnapshot['imageName']);
However, I can't seem to pass it to an Image.network()
widget. 但是,我似乎无法将其传递给
Image.network()
小部件。
Is it possible to display an image the way I'd like to, or should I use a different Image provider? 是否可以按照我想要的方式显示图像,还是应该使用其他图像提供程序?
You need the .getDownloadURL()
from the StorageReference imageLink
to get the storage URL link: 您需要来自StorageReference
imageLink
的.getDownloadURL()
来获取存储URL链接:
final imageUrl = await imageLink.getDownloadUrl();
Image.network(imageUrl.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.