1

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.

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

startAt expect a list as input. When I try to pass document Id it show need to add list as input.
I edited my answer, use startAfterDocument.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.