0

final Firestore = FirebaseFirestore.instance.collection("users").doc();

i have a collection users in my database ,using flutter(Dart), can anyone show me how to write a function who count the Numbers of users in This collection and print It on a Dashboard? thank you

ginoven
  • 5
  • 1
  • Check this out : https://stackoverflow.com/questions/53352632/how-to-get-the-number-of-firestore-documents-in-flutter – Sagar Acharya Feb 04 '22 at 09:23
  • 1
    You should note that the foreseen approach has a big drawback: you read all the documents of the collection each time you want to get the number of users and, therefore, **it has a cost**. Have a look at the following answer https://stackoverflow.com/questions/61250180/how-to-get-the-number-of-documents-under-a-firestore-collection/61250956#61250956 – Renaud Tarnec Feb 04 '22 at 09:37

1 Answers1

0
    static Future<int> totalFollowers({String userId}) async {
    final collection = FirebaseFirestore.instance
        .collection(ParamsArgus.COLLECTION_USERS)
        .snapshots();
    return collection.length;
    }
DholaHardik
  • 462
  • 1
  • 4
  • 7