我有一个名为 market 的集合,它有两个我可以在 firebase 中确认的文件。
我现在正在尝试获取该集合中的文档数量,如下所示:-
getAllItemsForSell()async {
final snapshot = await Firestore.instance.collection('market').getDocuments();
final documents = snapshot?.documents;
print(documents); // []
}
上面的函数打印出'[]',我做错了什么?
我在互联网上到处查看,但似乎没有任何帮助。
请帮忙?我是 Flutter Firebase 世界的新手,在此先感谢。
要查看该特定集合中的文档数量,请尝试从用户集合中获取 documentId 并通过将其传递给市场集合来对其进行迭代
int sampleNum =0;
getAllItemsForSell()async {
QuerySnapshot users = await Firestore.instance.collection('market').getDocuments();
for(DocumentSnapshot docs in users.documents){
QuerySnapshot doc = await Firestore.instance.collection('market').document(docs.documentID).collection('marketPosts').getDocuments();
sampleNum = doc.documents.length;
//posts = doc.documents.map((doc) => Post.fromDocument(doc)).toList();
}
print(sampleNum.toString() +' **********');
}