0

I am getting videos from firebase firestore by using this query:

FirebaseFirestore.instance.collection('videos').where('uid',isEqualTo: widget.uid).get();

I am getting the videos of particular user id but how can I get the length of videos that how many videos has been posted/stored by this particular current user 'uid'. I want to know the numbers of videos that user holds. So basically I need this to hide the container where I display videos, if there is no video by this user id.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
GAGAN SINGH
  • 261
  • 2
  • 17

1 Answers1

1

If I correctly understand your question, you should use the size property, as follows for example.

FirebaseFirestore.instance
.collection('videos')
.where('uid',isEqualTo: widget.uid)
.get()
.then((QuerySnapshot querySnapshot) {
    Print(querySnapshot.size);
});

HOWEVER, you should note that this implies that you read all the documents corresponding to the query each time you want to get the number of videos and, therefore, it has a cost. If your collection is and stays small, using the size method can be a good approach.

But if the collection is going to contain a lot of docs and you often need to get the number of docs, better use another approach, like maintaining one or more counters. See these answers for examples: 1, 2 and 3.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121