2

Using the Firebase powered CMS Flamelink, there is a certain Firestore and Storage structure provided by the CMS. Images are stored in the Storage and references to it in Firestore. Firestore does provide me a DocumentReference to the Asset in the storage. But I have no idea how to get the DownloadUrl from the DocumentReference. I can retrieve the filename from the DocumentReference, but not the full path in the Storage. The full path would allow me to create a StorageReference, which has access to getDownloadUrl().

So as a workaround I am currently concatenating the filename with the storage prefix I looked up in the storage. But there must be a better way to retrieve a DownloadUrl from a DocumentReference. Otherwise it is not really a DocumentReference in my opinion. How is the correct approach to get the DownloadUrl from a DocumentReference?

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;
    });
}

I am using Flutter 1.7.8 with cloud_firestore 0.12.8 and firebase_storage 3.0.3.

besserwisser
  • 2,590
  • 3
  • 13
  • 16

1 Answers1

2

I resolve this by adding populate to get method.

const app = flamelink({
  firebaseApp
});

const res = await app.content.get({ 
  schemaKey: 'schemaName', 
  populate:[{ field: 'image'}]
}).then( ... )

Then image object contains url property with absolute path and you don't need your getNewImage() function.

Ingrid Oberbüchler
  • 766
  • 2
  • 6
  • 16