3
votes

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');
      }
1
You need to Subscribe to uploadTask.event Stream to get the upload Status on Screen.anmol.majhail

1 Answers

33
votes

Newer version:

As the .onComplete has been removed from the upload task, you now simply need to await till the task is finished:

Reference ref = FirebaseStorage.instance.ref().child('your_path');
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();