NetworkingMedium30 XP3 min read
What are the different pagination strategies for REST APIs in Flutter?
TL;DR: Offset pagination: GET /items?page=2&limit=20. Cursor pagination: GET /items?after=cursor&limit=20. Keyset: GET /items?lastId=123. Cursor/keyset are better for real-time data — they don't skip items when new ones are inserted.
Full Answer
| Aspect | Offset/Page | Cursor |
|---|---|---|
| Query param | ?page=2&limit=20 | ?after=abc123&limit=20 |
| Stable results | No — inserts shift page boundaries | Yes — cursor is a fixed pointer |
| DB performance | Poor at large offsets (OFFSET 10000) | Good — indexed cursor lookup |
| Random access | Yes — jump to page 5 | No — must follow cursor chain |
| Best for | Admin tables, small datasets | Infinite scroll, social feeds |
🎯
For infinite scroll in Flutter, always use cursor pagination. Offset pagination causes duplicate or missing items when new content is posted between pages.
Code Examples
dartCursor pagination with Bloc
Output
// First call: cursor=null -> items 1-20, cursor='abc123' // Next call: cursor='abc123' -> items 21-40, cursor='def456'
Common Mistakes
- ✗Resetting to page=1 on pull-to-refresh — this shows duplicate items if new data was added
- ✗Not handling the end state (hasMore=false) — continues calling API returning empty arrays
Interview Tip
💡
Explain why cursor beats offset: if 5 items are added to the top of the feed between page 1 and page 2, offset-based pagination shows 5 duplicate items. Cursor pagination is immune to this.
#pagination#cursor#offset#infinite-scroll#page-based