How to make Count query to Firebase in Flutter??

· 3 min read
How to make Count query to Firebase in Flutter.
How to make Count query to Firebase in Flutter.

Hello Guys, Earlier we have been through Step By Step Guide How to integrate Firebase in a Flutter Mobile Application. So getting into more details will help us to How to make Count query to Firebase in Flutter.

How to make Count query to Firebase in Flutter??

Answer: There are no inbuilt Count queries in a Firebase.

Hence your options are.

  • Retrieve all data matching your query and count client-side
  • Keep a separate count-node that you update whenever you add/remove items.

Let’s see the first option: Retrieve all data matching your query and count client-side

How to get data from Firebase in Flutter?

We will be using StreamBuilder<QuerySnapshot>() to get data from Firebase.

What is Stream Builder in Flutter?

StreamBuilder is a StatefulWidget that responds to the asynchronous procession of data. In other words, we can say that it is able to keep a ‘running summary’ and or record and note the ‘latest data item’ from a stream of data.

It will have two parameters.

  • A Stream,
  • A Builder,

A Builder is basically used to convert a Stream of data into Widgets.

Consider a StreamBuilder<QuerySnapshot>() as below:

StreamBuilder<QuerySnapshot>(
                        stream: FirebaseFirestore.instance
                            .collection('Task')
                            .where('userID',isEqualTo: Constants.loggedInUserID).
                            snapshots(),
                        builder: (context, snapshot) {
                          if (!snapshot.hasData) {
                            return Center(
                              child: Text("No Task Added"),
                            );
                          }
                          final documentSnapshotList = snapshot.data.docs;
              print(documentSnapshotList.length);
                          
                          // return 
                        },
                      ),

In the Code Snippet above we have used a Stream as

stream: FirebaseFirestore.instance.collection('Task').where('userID',isEqualTo: Constants.loggedInUserID).snapshots(),

Where we have to get data from Collection called Task and along with it, we have used a Where Clause to apply specific conditions based on user criteria. We will be getting a list of all  data into

final documentSnapshotList = snapshot.data.docs;

Then we have used a length to get the length of all data.

Let’s see how the second option: Keep a separate count-node that you update whenever you add/remove items.

Users can create a Count Field in a Model and keep it and when using data for the first time increase its value by 1 and update it every time whenever users add or remove items. Consider a code snippet like below:

getCount(){
var updateCount = task.reference.updateData({'Task': task.taskCount+1})
}

Conclusion:

So in this article, we have been through How to make Count query to Firebase in Flutter.

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply