Flutter:获取FirebaseStorage下载URL和上传状态
我是新来的。我正在尝试获取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');
}
回答:
最新版本的插件不允许您再使用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();
以上是 Flutter:获取FirebaseStorage下载URL和上传状态 的全部内容, 来源链接: utcz.com/qa/413849.html