3

I'm new to flutter. I am trying to get StorageUploadTask status and have the Download when is status isCompleted & isSuccessful. The examples I found online are of the old version:

StorageUploadTask uploadTask = ref.putFile(avatarImageFile);
Uri downloadUrl = (await uploadTask.future).downloadUrl;

The above doesn't work for the new firebase_storageplugin version. Please help. Below is my code so far.

StorageUploadTask uploadTask = ref.putFile(avatarImageFile);

    StorageReference downRef = uploadTask.lastSnapshot.ref;
    String downloadUrl = await downRef.getDownloadURL();

    if(uploadTask.isComplete) {
      if(uploadTask.isSuccessful) {
        print('Upload Successful');
      } else if(uploadTask.isCanceled) {
        print('Upload Cancelled');
      } else {
        print('${uploadTask.lastSnapshot.error}');
      }
    } else if(uploadTask.isInProgress){
        print('Upload in Progress');
      } else if(uploadTask.isPaused) {
        print('Upload Paused');
      }
KENdi
  • 7,576
  • 2
  • 16
  • 31
Tayo.dev
  • 1,546
  • 3
  • 21
  • 31

1 Answers1

33

Newer version:

As the .onComplete has been removed, now you just need to await until the task is finished:

final ref = FirebaseStorage.instance.ref('images/foo.png');
await ref.putFile(fileToUpload);
String url = await ref.getDownloadURL(); // <-- This is your download url.

Older version:

The older version of plugin doesn't let you use task.future() anymore and in the documentation they say to use lastSnapshot which didn't work for me. So, I used onComplete. Here is the working solution:

var ref = FirebaseStorage.instance.ref().child("your_path");
var uploadTask = ref.putFile(avatarImageFile);
var storageTaskSnapshot = await uploadTask.onComplete;
var downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • 3
    this does not seems to be working now, please provide the updated answer. there is nothing like onComplete – Jatin Pandey Nov 08 '20 at 07:15
  • With the new version a simple await should do the trick on the trick: final Reference reference = FirebaseStorage.instance .ref().child("your_path"); await reference.putFile(fileToUpload); return await reference.getDownloadURL(); – Benjamin Corben Nov 18 '20 at 17:31