如何使用 Flutter 在 Firestore 中删除集合中的所有文档 [英] How to Delete all documents in collection in Firestore with Flutter

查看:29
本文介绍了如何使用 Flutter 在 Firestore 中删除集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Firestore 数据库.我的项目插件:

I have a firestore database. My project plugins:

cloud_firestore:^0.7.4firebase_storage: ^1.0.1

cloud_firestore: ^0.7.4 firebase_storage: ^1.0.1

这有一个包含多个文档的消息"集合.我需要删除消息集合中的所有文档.但是这段代码失败了:

This have a collection "messages" with a multiple documents. I need to delete all documents in the messages collection. But this code fail:

Firestore.instance.collection('messages').delete();

但删除不是定义

正确的语法如何?

推荐答案

啊.第一个答案几乎是正确的.这个问题与 dart 中的 map 方法以及它如何与 Futures 一起工作有关.无论如何,尝试使用 for 循环而不是这样,你应该会很好:

Ah. The first answer is almost correct. The issue has to do with the map method in dart and how it works with Futures. Anyway, try using a for loop instead like so and you should be good:

firestore.collection('messages').getDocuments().then((snapshot) {
  for (DocumentSnapshot ds in snapshot.documents){
    ds.reference.delete();
  });
});

这篇关于如何使用 Flutter 在 Firestore 中删除集合中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆