Reputation: 291
So I am using Firebase to retrieve data from firestore, And this is working fine, But now in order to save money and resources I am using a limit of 40 items, so only 40 items come from firebase, but now when the user reaches the end of the list i want the user to be able to get the next 40 items from the firebase data base. And that is what i dont know how to do. To get the next 40 items without having to read the whole 80 items from firebase..
This is my code:
getCountryItems() async{
QuerySnapshot snapshot = await userCountry
.orderBy('timeStamp', descending: true)
.limit(40) //getting only 40 items
//.orderBy('likesCount', descending: true)
.getDocuments();
List<Items> countryPosts = snapshot.documents.map((doc) => Items.fromDocument(doc)).toList();
setState(() {
this.countryPosts = countryPosts;
});
}
So that is what is getting the First 40 items now i want to get the 40 items after this only after a button is pressed:
FlatButton(
onPressed: (){}//funtion to get the next 40 items
);
Upvotes: 1
Views: 73
Reputation: 15268
Reference: https://firebase.google.com/docs/firestore/query-data/query-cursors
Use startAfter to move the cursor to desired position
getCountryItems(paginateAt = 0) async{
QuerySnapshot snapshot = await userCountry
.orderBy('timeStamp', descending: true)
.startAfter(paginateAt)
.limit(40) //getting only 40 items
//.orderBy('likesCount', descending: true)
.getDocuments();
List<Items> countryPosts = snapshot.documents.map((doc) => Items.fromDocument(doc)).toList();
setState(() {
this.countryPosts = countryPosts;
});
}
FlatButton(
onPressed: (){
getCountryItems(this.countryPosts.last)
}//funtion to get the next 40 items
);
Upvotes: 1