我有一个火库数据库。我的项目插件:
云
_
firestore:^0.7.4 firebase
_
存储:^1.0.1
这有一个具有多个文档的集合"messages“。我需要删除消息集合中的所有文档。但是这段代码失败了:
Firestore.instance.collection('messages').delete();
但没有定义delete
正确的语法是什么?
发布于 2020-04-05 15:29:20
我认为这可能有助于在任何文档引用中使用多个集合
// add ${documentReference} that contains / may contains the ${collectionsList}
_recursiveDeleteDocumentNestedCollections(
DocumentReference documentReference, List collectionsList) async {
// check if collection list length > 0
if (collectionsList.length > 0) {
// this line provide an async forEach and wait until it finished
Future.forEach(collectionsList, (collectionName) async {
// get the collection reference inside the provided document
var nfCollectionRef = documentReference.collection(collectionName);
try {
// get nested collection documents
var nestedDocuemtnsQuery = await nfCollectionRef.getDocuments();
// loop through collection documents
Future.forEach(nestedDocuemtnsQuery.documents, (DocumentSnapshot doc) async {
// recursive this function till the last nested collections documents
_recursiveDeleteDocumentNestedCollections(
doc.reference, collectionsList);
// delete the main document
doc.reference.delete();
});
} catch (e) {
print('====================================');
print(e);
print('====================================');
}
});
}
}
https://stackoverflow.com/questions/53089517
复制相似问题