英文:
Canceling a Firebase Storage UploadTask triggers error
问题
我正在尝试将一个zip文件上传到Firebase存储,但我也想能够"取消"这个上传,所以我创建了一个静态的UploadTask(ValueNotifier<UploadTask>)变量,如下所示:
SettingsUtils.uploadStatus.value = ref.putFile(targetZipFile);
但是,当我尝试调用cancel
方法(SettingsUtils.uploadStatus.value.cancel()
)时,会出现以下异常:
我做错了什么?如何正确处理这个异常?
英文:
I'm trying to upload a zip file to Firebase Storage but I also want to be able to "Cancel" this upload, so I created a Static UploadTask (ValueNotifier<UploadTask>) variable like this:
SettingsUtils.uploadStatus.value = ref.putFile(targetZipFile);
But, when I try to invoke the method cancel ( SettingsUtils.uploadStatus.value.cancel()
), this gives me the following exception :
I'm doing something wrong? How can I treat this exception properly?
答案1
得分: 1
I think this is the default behavior, see here:
> In addition to starting uploads, you can pause, resume, and cancel
> uploads using the pause(), resume(), and cancel() methods. Pause and
> resume events raise pause and progress state changes respectively.
> Canceling an upload causes the upload to fail with an error indicating
> that the upload was canceled.
Wrap it inside a try catch and check if the code is "canceled". If yes, it worked.
What I also found, you can listen to different states like this:
uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) {
switch (taskSnapshot.state) {
case TaskState.running:
final progress =
100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
print("Upload is $progress% complete.");
break;
case TaskState.paused:
print("Upload is paused.");
break;
case TaskState.canceled:
print("Upload was canceled");
break;
case TaskState.error:
// Handle unsuccessful uploads
break;
case TaskState.success:
// Handle successful uploads on complete
// ...
break;
}
}
From the official docs: https://firebase.google.com/docs/storage/flutter/upload-files
英文:
I think this is the default behavior, see here:
> In addition to starting uploads, you can pause, resume, and cancel
> uploads using the pause(), resume(), and cancel() methods. Pause and
> resume events raise pause and progress state changes respectively.
> Canceling an upload causes the upload to fail with an error indicating
> that the upload was canceled.
Wrap it inside a try catch and check if the code is "canceled". If yes, it worked.
What i also found, you can listen to differen states like this:
uploadTask.snapshotEvents.listen((TaskSnapshot taskSnapshot) {
switch (taskSnapshot.state) {
case TaskState.running:
final progress =
100.0 * (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes);
print("Upload is $progress% complete.");
break;
case TaskState.paused:
print("Upload is paused.");
break;
case TaskState.canceled:
print("Upload was canceled");
break;
case TaskState.error:
// Handle unsuccessful uploads
break;
case TaskState.success:
// Handle successful uploads on complete
// ...
break;
}
From the official docs: https://firebase.google.com/docs/storage/flutter/upload-files
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论