Flutter:获取FirebaseStorage下载URL&上传状态 [英] Flutter: Get FirebaseStorage Download URL & Upload Status

查看:95
本文介绍了Flutter:获取FirebaseStorage下载URL&上传状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。我正在尝试获取 StorageUploadTask 状态,并在状态为 isCompleted 的状态下下载。 成功。我在网上找到的示例都是旧版本的

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;

以上内容不适用于新的 firebase_storage 插件版本。请帮忙。

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


推荐答案

最新版本的插件没有不要再使用 task.future()了,在文档中他们说要使用 lastSnapshot 为我工作。因此,我使用了 onComplete 。这是可行的解决方案:

The latest 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();

这篇关于Flutter:获取FirebaseStorage下载URL&上传状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆