一尘不染

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

flutter

我是新来的。我正在尝试获取StorageUploadTask状态,并在状态为isCompleted&时下载isSuccessful。我在网上找到的示例是旧版本:

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

阅读 524

收藏
2020-08-13

共1个答案

一尘不染

最新版本的插件不允许您再使用task.future(),在他们说要使用的文档中,lastSnapshot这对我不起作用。所以,我用了onComplete。这是有效的解决方案:

var ref = FirebaseStorage.instance.ref().child("your_path");
var uploadTask = ref.putFile(avatarImageFile);
var storageTaskSnapshot = await uploadTask.onComplete;
var downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
2020-08-13