I have a list of mentors in firebase realtime database. And each of them has 'numFav' which indicates how many users have checked him as favorite.
In my mentor list page, I want to show registered mentors in the order of descending 'numFav'. At the same time, I want to paginate.
So I wrote code for initial fetch and load more.
Future<List<MentorModel>> initialMentorFuture(int num) {
final future = _database.orderByChild('numFav').limitToLast(num).once().then((snapshot) {
List<MentorModel> _mentorList = [];
Map<String, dynamic>.from(snapshot.value).forEach((key, value) => _mentorList.insert(0, MentorModel.fromRTDB(key, value)));
return _mentorList;
});
return future;
}
Future<List<MentorModel>> moreMentorFuture(int num, int startAtValue) {
final future = _database.orderByChild('numFav').startAt(startAtValue).limitToLast(num).once().then((snapshot) {
List<MentorModel> _mentorList = [];
Map<String, dynamic>.from(snapshot.value).forEach((key, value) => _mentorList.insert(0, MentorModel.fromRTDB(key, value)));
return _mentorList;
});
return future;
}
initialMentorFuture works as intended. moreMentorFuture doesn't. I have two problems.
I'm using descending order, but limitToLast and startAt seems to contradict each other.
There could be many mentors with same 'numFav'. For example if the last mentor in the first page had numFav of 10, I can't just say startAt(10) in moreMentorFuture because there could be other mentors with numFav of 10 that has not been displayed in the first page.
I'm pretty sure I'm not the only one trying to sort a list by number of likes. Anyone know how to solve this issue?
EDITED
One more question: Does pagination really save loading time? When I call .orderByChild('numFav').limitToLast(10)
, how many data are actually downloaded from server? If I call orderByChild()
does it download the whole data set for sorting? If so, what's the point of pagination?