Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Firestore – get only 10 docs with their id

here I’m trying to get the top 10 names only from my firebase firestore,
and I searched on how I do it with the listview that I have, but I get to nowhere.
so I thought about getting the id of my documents instead.
In my firestore I gave the top 10 documents IDs from 1 to 10, now I’m stuck and I have no idea how to do it. please help.

static int id = 1;
StreamBuilder<QuerySnapshot>(
      stream: fireStore.collection(path).snapshots(),
      builder: (context, snapshot) {
        if(!snapshot.hasData){
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.blueAccent,
            ),
          );
        }
        List<InfoBox> ancients = [];
        try {
          final collection = snapshot.data!.docs;
          for (var collect in collection) {
            final name = collect['Name'];
            final image = collect['Image'];
            final collectionWidget = InfoBox(
                ancientName: name,
                ancientImage: image
            );
            ancients.add(collectionWidget);
          }
        }catch(e){
          print('problems in stream builder \n error : $e');
        }
        return Expanded(
            child:ListView(
              children: ancients,
            )
        );
      },
    );

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are probably searching for limit() function. According to the documentation:

To limit the number of documents returned from a query, use the limit method on a collection reference

You can implement it like this:

fireStore.collection(path).limit(10).snapshots();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading