Ragawender Sivanraj
Ragawender Sivanraj

Reputation: 37

retrieve data from firestore with flutter

I wanted to retrieve data from firestore to flutter app. I have a lot of data. So I don't wanted to all the data at once. I wanted to get first 10 documents at once and when press load more, it should give another 10 documents so on. I tried this way.

var myWhishlist;
late QuerySnapshot snap;
late CollectionReference userDoc;
retrieve() async {
  userDoc = FirebaseFirestore.instance
      .collection('user/' + Uid + "/myItems");
  snap = await userDoc.where('isPaid', isEqualTo: false).limit(10).get();
  myWhishlist = snap.docs.map((e) => e.id).toList();
}

In this way I got first 10 documents. How to get next 10 documents? IS there any method available to get like this? Can Some one help me?

Thanks in advance.

Upvotes: 1

Views: 158

Answers (2)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12343

What you are doing for now is correct, your code will limit the results to 10. If you want more results, you can use or startAfterDocument and pass the document of the last document retrieved from your previous fetch.

I would suggest creating a standalone method, which is

Future<QuerySnapshot> example (DocumentSnapshot? document) async {...etc}

If you don't pass a string to it, run your ordinary query using limit. If a string is passed, use the startAfterDocument flag.

snap = await userDoc.where('isPaid', isEqualTo: false).limit(10).startAfterDocument(documentSnapshot).get();

Upvotes: 1

Mozes Ong
Mozes Ong

Reputation: 1294

take a look at paginate_firestore package https://pub.dev/packages/paginate_firestore

Upvotes: 1

Related Questions