Leon Lo
Leon Lo

Reputation: 587

Flutter & Firestore - problem about "startAfter" pagination

I am able to use "startAfter" and "limit" to do pagination but it have bug.

For example, in Firestore DB I have 7 records:

{"title": "item1", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item2", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item3", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item4", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item5", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item6", "create_datetime": "2018-11-11 11:11:11"}
{"title": "item7", "create_datetime": "2018-12-22 22:22:22"}

When the page size is 5, first page is ok because I used:

.orderBy('create_datetime').limit(5)

It gives me item 1-5.

When it load second page, I used:

.orderBy('create_datetime').startAfter(['2018-11-11 11:11:11']).limit(5)

The problem is that the second page result had item7 only, item6 was disappeared. "startAt" have the same problem too.

I really hope it has "offset" function. Does anyone have solution?

Upvotes: 1

Views: 2427

Answers (2)

shashikant durge
shashikant durge

Reputation: 139

Currently flutter don't support startAfter(lastDocFetched) or startAt(anyDoc). This is required as you said it starts with the string matching values, not at a particular document.

Upvotes: 0

Bongani Sibiya
Bongani Sibiya

Reputation: 101

Your query will always return 'item7' because you always starting after 2018-11-11 11:11:11 so it will ignore all the other items and go to the last 2018-11-11 11:11:11 and skip from there. You need to get the last item returned and keep a reference to it, then on your startAfter use the document reference to start after. Usually, flutter's startAfter requires a list to keep a reference to it.

Look at Firebase query cursors

Upvotes: 1

Related Questions