Canceling a Firebase Storage UploadTask triggers error

huangapple go评论61阅读模式
英文:

Canceling a Firebase Storage UploadTask triggers error

问题

我正在尝试将一个zip文件上传到Firebase存储,但我也想能够"取消"这个上传,所以我创建了一个静态的UploadTask(ValueNotifier<UploadTask>)变量,如下所示:

SettingsUtils.uploadStatus.value = ref.putFile(targetZipFile);

但是,当我尝试调用cancel方法(SettingsUtils.uploadStatus.value.cancel())时,会出现以下异常:

Canceling a Firebase Storage UploadTask triggers error

我做错了什么?如何正确处理这个异常?

英文:

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 :

Canceling a Firebase Storage UploadTask triggers error

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(&quot;Upload is $progress% complete.&quot;);
      break;
    case TaskState.paused:
      print(&quot;Upload is paused.&quot;);
      break;
    case TaskState.canceled:
      print(&quot;Upload was canceled&quot;);
      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

huangapple
  • 本文由 发表于 2023年5月24日 20:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定