flutter 如何检查有多少文件可用的firebase

ojsjcaue  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(105)

当我的查询条件匹配时,如何检查firebase中有多少文档可用,并且我想打印出来

FirebaseFirestore firestore = FirebaseFirestore.instance;
CollectionReference<Map<String, dynamic>> collectionReference =
    await firestore.collection('stations');

var setNo = collectionReference
    .where('place', isEqualTo: userDistrict.toLowerCase())
    .where('norequest', isGreaterThanOrEqualTo: 10)
    .snapshots();

var totalEquals = await collectionReference.snapshots().length;
print("documnet afthal :${totalEquals}");

setState(() {
  userLocationSetsNo = totalEquals;
});
ar7v8xwq

ar7v8xwq1#

以这个集合为例:

CollectionReference CollectionRef =
   FirebaseFirestore.instance.collection('your-collection-name');

在我们从Firebase Firestore获得它的QuerySnapshot之后:

QuerySnapshot querySnapshot = await 
 CollectionRef
        .where('field-name', isEqualTo:'condition')
        .get();

我们可以简单地使用它的docs属性,该属性将返回包含所有集合文档的List,然后使用length属性获取该List的长度:

print(querySnapshot.docs.length);

希望这对你有帮助:)

相关问题