使用基于Firebase的CMS Flamelink,CMS提供了某种Firestore和Storage结构。图像存储在存储中,并在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来获取方法来解决此问题。

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/

10-09 03:23