使用 Firebase 支持的 CMS Flamelink,CMS 提供一定的 Firestore 和存储结构。图像存储在存储中,对其的引用存储在 Firestore 中。 Firestore 确实为我提供了存储中 Assets 的 DocumentReference。但我不知道如何从 DocumentReference 获取 DownloadUrl。我可以从 DocumentReference 中检索文件名,但不能检索存储中的完整路径。完整路径将允许我创建一个 StorageReference,它可以访问 getDownloadUrl()。
因此,作为一种解决方法,我目前正在将文件名与我在存储中查找的存储前缀连接起来。但必须有更好的方法从 DocumentReference 检索 DownloadUrl。否则,我认为它并不是真正的 DocumentReference。从 DocumentReference 获取 DownloadUrl 的正确方法是什么?
getNewImage(DocumentReference imgRef) async {
DocumentSnapshot imgSnapshot = await imgRef.get();
final imageName = imgSnapshot.data['file'];
// How to get path dynamically?
String storagePath = 'flamelink/media/$imageName';
StorageReference storageReference = await DataProvider.getStore();
StorageReference ref = storageReference.child(storagePath);
String dlurl = await ref.getDownloadURL();
setState(() {
_imageUrl = dlurl;
});
}
我正在使用 Flutter 1.7.8 以及 cloud_firestore 0.12.8 和 firebase_storage 3.0.3。
最佳答案
我通过添加 populate
到 get 方法来解决这个问题。
const app = flamelink({
firebaseApp
});
const res = await app.content.get({
schemaKey: 'schemaName',
populate:[{ field: 'image'}]
}).then( ... )
然后图像对象包含具有绝对路径的 url
属性,并且您不需要 getNewImage() 函数。
关于flutter - 无法从 Flutter Firebase 中的 DocumentReference 获取 DownloadURL 或 StorageReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376352/