首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >文一删除了我的文档,相应的图像仍然保存在存储中。

文一删除了我的文档,相应的图像仍然保存在存储中。
EN

Stack Overflow用户
提问于 2021-11-06 21:46:39
回答 1查看 127关注 0票数 0

我是新来的颤栗,我正面临一个问题,是如何删除菲利还原照片。

我已经创建了一个子集合,在其中存储我的产品详细信息,该产品还包含一个存储在Firebase中的映像。如果删除我的产品,就像我有10个产品,但我想删除一个特定的产品,当我按删除按钮时,它正在删除我的产品,这是一个文档,但它没有从Firebase存储中删除相应的图像

我尝试了一些方法,但它不起作用,它只是删除了我的云修复文档,但是照片仍然保存在Firestore中。

我正在使用图像URL来显示照片。我希望当我点击我的删除按钮时,它也会从Firebase中删除相应的图像。因此,我使用它来删除我的云修复文档。

代码语言:javascript
运行
复制
TextButton(
  onPressed: () async {
    await FirebaseFirestore.instance
        .collection('users')
        .doc(LoginUser!.uid)
        .collection('products')
        .doc(document.id)
        .delete()
        .then((value) async {
      //await FirebaseStorage.instance.ref().child('productImage').child('productImageUrl').delete();
      
      
      print(
          'Product Deleted But image is still there in Storage Folder');
    });
  },
  child: Text('Delete')),

我使用这段代码在云修复中保存我的图像Url

代码语言:javascript
运行
复制
final productImgRef = FirebaseStorage.instance
    .ref()
    .child('productImage')
    .child(FirebaseUser.uid +time +'.jpg');
await productImgRef.putFile(pickImageFile!);
var productImageUrl = await productImgRef.getDownloadURL();

await FirebaseFirestore.instance
    .collection('users')
    .doc(FirebaseUser.uid)
    .collection('products')
    .add({
  'productName': _productName,
  'productDes': _productDes,
  'bidPrice': _bidPrice,
  'auctionDate': _auctionDate,
  'submitBy': userName,
  'productImageUrl': productImageUrl,
});
EN

回答 1

Stack Overflow用户

发布于 2021-11-06 23:32:39

假设这个注释行是您尝试过的:

代码语言:javascript
运行
复制
await FirebaseStorage.instance.ref().child('productImage').child('productImageUrl').delete();

关于'productImageUrl'的引号是错误的。使用这段代码,它尝试删除名为productImageUrl的图像,这不是您的图像的名称。

最起码应该是:

代码语言:javascript
运行
复制
await FirebaseStorage.instance.ref().child('productImage').child(productImageUrl).delete();

通过从productImageUrl周围删除引号,代码将使用变量的

但更有可能的是,productImageUrl包含一个下载URL,在这种情况下,您首先需要通过调用refFromURL方法将其映射回存储Reference

代码语言:javascript
运行
复制
var storageRef = FirebaseStorage.instance.refFromURL(productImageUrl);
await storageRef.delete();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69868149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档