How can I check the length of a firebase document using stream builder

860

Solution 1

You need to use the docs property, which "returns a List containing DocumentSnapshot classes", as follows:

return userActivitiesSnapshot.docs.length;

Solution 2

To get a stream of documents, you need to use the .snapshots() method which returns a Stream of QuerySnapshot instead of the .getDocuments() (deprecated in favor of .get()) method which returns a Future of QuerySnapshot.

Then you can map Stream<Snapshot> into a stream of the length of the snapshot's documents.

Your numActivities method should look like this:

 static Stream<int> numActivities(String userId) {
    return activitiesRef
        .document(userId)
        .collection('userActivities')
        .where('seen', isEqualTo: '')
        .snapshots()
        .map((documentSnapshot) => documentSnapshot.docs.length);
  }

Using this in your use case, you need to listen to the stream and update the _activityCount variable.

 _setUpactivityCount() async {
  final String currentUserId =
      Provider.of<UserData>(context, listen: false).currentUserId;
  DatabaseService.numActivities(currentUserId).listen((activityCount) {
    if (mounted) {
      setState(() {
        _activityCount = activityCount;
      });
    }
  });
}

Make sure you take care of _activityCount being null in it's initial state.

Share:
860
Enam
Author by

Enam

Updated on December 23, 2022

Comments

  • Enam
    Enam over 1 year

    In my flutter firebase app, I am able to get the length of a user's activity document in firebase using a query snapshot. However, I want the number of documents to update in real-time without the user needing to refresh the page. Can I do that by converting the codes below using stream builder to get the real-time length and how can I do that? this is the code am using now which works perfectly well but doesn't update in real-time.

     //this is the code I want to convert to stream 
        //builder.
      static Future<int> numActivities(String userId) 
         async {
         QuerySnapshot userActivitiesSnapshot = await 
          activitiesRef
        .document(userId)
        .collection('userActivities')
        .where('seen', isEqualTo: '')
        .getDocuments();
        return userActivitiesSnapshot.documents.length;
        }
    
    • Emmanuel
      Emmanuel over 3 years
      Is it possible for you to mark as Accepted the answer that helped you to solve your problem? This would help the community to notice what's the best approach according to your question by giving visibility of the best answer.
  • Enam
    Enam over 3 years
    thank you for your contribution, this is the second time you have contributed to helping me solve a problem and I appreciate it
  • Enam
    Enam over 3 years
    thank you very much. but I think am facing a new problem using your method above. am getting the error Stream<int> cannot be assigned to a variable of type int. I am using a method on the feed page, in initSate to get the stream value. I have just added the new Code.
  • Victor Eronmosele
    Victor Eronmosele over 3 years
    You need to listen to the stream events and you can get the actual value from there.
  • Enam
    Enam over 3 years
    Thank you very much.Been trying to figure this out for some time now. You cannot imagine how grateful I am.
  • Victor Eronmosele
    Victor Eronmosele over 3 years
    Glad I could help :)