首页 > 解决方案 > 如何在 Flutter 中从 Firebase 存储中删除文件

问题描述

我尝试了这篇文章中的示例How to delete a Firebase Storage file with flutter?

但它没有用。这是我尝试过的:

String filePath = 'https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F1579839318515.jpg?alt=media&token=cd8880eb-8b37-45e4-8dc1-e75de0c5f7cb'
  .replaceAll(new RegExp(r 'https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F'), '').split('?')[0];

FirebaseStorage.instance.ref().child(filePath).delete()
  .then((_) {
    print('Successfully deleted $filePath storage item');
  }).catchError((e) {
    print("err: $e");
  });

这就是它返回的内容:

I / flutter(4920): err: PlatformException(deletion_error, Object does not exist at location., null)

可以对它返回错误的原因提出一些想法,因为它与示例完全相同。谢谢阅读。

标签: fluttergoogle-cloud-storagefirebase-storage

解决方案


Firebase SDK 采用存储它们的存储桶中文件的路径。他们不采用 URL 或拆分 URL。它们也不采用 url 编码的字符串。路径必须是用于上传它们的纯字符串。

只是一个猜测,但如果你有以开头的下​​载 URL

https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F1579839318515.jpg "

那么文件的字符串路径大概就是

分类图像/1579839318515.jpg

这就是您应该传递的内容FirebaseStorage.instance.ref().child()以构建使用它的引用(因为 %2F 是 URL 中正斜杠的编码版本)。


推荐阅读