Martin Seubert
Martin Seubert

Reputation: 1028

Flutter get Download Url from Firebase Storage

I am trying to load a thumbnail image from Firebase Storage, to improve the performance of my app. Once the thumbnail is loaded, the actual image should be displayed. For that reason I implemented a Cloud Function, which automatically uploads a thumbnail for each image.

I have no Problem with getting the download url from the uploaded image. But because of the reason, that the generation of the thumbnail takes 3 sec, I can not automatically retrieve the download Url from the thumbnail.

Here is what I came up so far:

   Future uploadImage(var imageFile) async {

 var uuid = new Uuid().v1();
 StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
 StorageUploadTask uploadTask = ref.putFile(imageFile);
 String downloadUrl = await (await uploadTask.onComplete).ref.getDownloadURL();

 StorageReference reff = FirebaseStorage.instance.ref().child("thumb_post_$uuid.jpg");
 String url = (await reff.getDownloadURL()).toString();

 setState(() {
   downloadUrlThumb = url;
   downloadUrlFull = downloadUrl;
 });
}

Any idea on how I can wait for the uploaded thumbnail or on how I can delay the getDownloadURL() method for the downloadUrlThumb()?

Upvotes: 1

Views: 3243

Answers (2)

Sean Urgel
Sean Urgel

Reputation: 274

In the cloud function you can save the url into firestore after the thumbnail has been generated.

example: After record has been saved

{
   hello: "world",
   imageUrl: "asldnbalskd.firebase.storage.com"
}

around 5 seconds later when thumbnail is generated

{
   hello: "world",
   imageUrl: "asldnbalskd.firebase.storage.com",
   imageUrlThumbnail: "thumbnail-asldnbalskd.firebase.storage.com"
}

In the flutter side you can just listen to the document for any changes.

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658017

You could write a record into the database when thumbnail creation is completed
and subscribe to updates for that table to get notified in the client app when the record was added.
If you write the path of the uploaded file (or the thumbnails path) to the database you can identify which upload/thumbnail-creation was completed.

Upvotes: 2

Related Questions