Flutter:获取FirebaseStorage下载URL和上传状态

时间:2018-10-08 17:48:36

标签: firebase dart flutter firebase-storage

我是新手。我正在尝试获得StorageUploadTask的状态,并在状态为isCompletedisSuccessful时拥有“下载时间”。我在网上找到的示例都是旧版本:

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

以上内容不适用于新的firebase_storage插件版本。请帮忙。下面是到目前为止的代码。

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 个答案:

答案 0 :(得分:12)

最新版本的插件不允许您再使用task.future(),并且在文档中他们说使用lastSnapshot不适用于我。因此,我使用了onComplete。这是有效的解决方案:

StorageUploadTask uploadTask = ref.putFile(avatarImageFile);
StorageTaskSnapshot storageTaskSnapshot = await uploadTask.onComplete;
String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();