Flutter / FireStore:如何在Flutter中显示Firestore中的图像?
问题内容:
我想将我在应用程序中使用的某些图像放到Firestore中,然后从那里显示它们,而不是将它们作为资产捆绑在我的应用程序中。
为此,我想出了以下解决方案:对于要显示图像的项目,我创建了一个Firebase文档,其中有一个字段,用于存储存储相应图片的文件的名称:
document
- name
- description
- imageName
然后,我以相同的名称将图像上传到FireStore。
当我想显示图像时,我可以通过StreamBuilder成功获取文档,并提取imageName属性。
我还创建了必要的FireStore参考:
FirebaseStorage storage = new FirebaseStorage(
storageBucket: 'gs://osszefogasaszanhuzokert.appspot.com/'
);
StorageReference imageLink = storage.ref().child('giftShopItems').child(documentSnapshot['imageName']);
但是,我似乎无法将其传递给Image.network()
小部件。
是否可以按照我想要的方式显示图像,还是应该使用其他图像提供程序?
问题答案:
您需要.getDownloadURL()
从StorageReference imageLink
获得存储URL链接:
final imageUrl = await imageLink.getDownloadUrl();
Image.network(imageUrl.toString());