如何使用 Flutter 删除 Firestore 中具有特定值的多个文档

How to delete multiple documents with specific Value in Firestore with Flutter

我正在尝试从我的集合中删除字段中具有特定字符串值的多个文档。 但是我觉得我做错了什么。

onPressed: () async {
  Firestore.instance.collection('collection').getDocuments().then((snapshot) {
  for (DocumentSnapshot ds in snapshot.documents.where(('field') == 'specificValue'){
    ds.reference.delete();
    });
  });
}

我目前正在尝试在获取所有文档后循环删除每个项目,但我无法删除特定项目。因为我在“where”部分遇到错误。

要使用 where,您需要获取 collection 中所有文档的列表,然后 where 过滤 List 并删除filteredList

  onPressed: () async {
    onPressed() async {
    Firestore.instance.collection('collection').getDocuments().then((snapshot) {
      List<DocumentSnapshot> allDocs = snapshot.documents;
      List<DocumentSnapshot> filteredDocs =  allDocs.where(
              (document) => document.data['field'] == 'specificValue'
      ).toList();
      for (DocumentSnapshot ds in filteredDocs){
        ds.reference.updateData({
          'field': 'newValue'
        });
      }
    });
  }