Introduction
Pagination is essential when dealing with large datasets. Instead of loading everything at once, you load data in chunks (pages) as the user scrolls. GetX makes it easy to implement pagination with reactive state, handling loading indicators, error states, and pull-to-refresh. This guide covers common patterns for pagination using GetX.
Basic Pagination with Reactive State
Create a controller that holds the list of items, the current page, and a flag to indicate if more data is available. Use reactive variables so the UI updates automatically.
UI: Infinite Scrolling
In your view, use a ListView.builder with a ScrollController. When the user reaches the end, trigger the next page load.
Using StateMixin for Pagination
StateMixin can help manage the loading, error, and success states for the entire pagination flow. The list itself is part of the state, and we can still load more without changing the state type.
Pull-to-Refresh with Pagination
Combine RefreshIndicator with your paginated list to allow the user to manually refresh and reset the pagination state.
Error Handling & Retry
When a page load fails, you can show an error message and provide a retry button. For a better user experience, you can keep the existing items and allow the user to retry the failed page.
Best Practices
- Use
addListeneronScrollController– Detect when the user reaches the end of the list. - Dispose the
ScrollController– Prevent memory leaks in your view. - Avoid loading more while already loading – Check
isLoadingMoreto prevent duplicate requests. - Reset pagination on refresh – Clear the list and reset the page counter.
- Show a loading indicator at the bottom – Provide visual feedback when loading the next page.
- Handle edge cases – Empty lists, no more data, and network errors gracefully.
Common Mistakes
- ❌ Not checking if
hasMorebefore loading – May attempt to load beyond the last page. ✅ CheckhasMoreand stop when no more data. - ❌ Calling
fetchItemsmultiple times in quick succession – Causes duplicate requests. ✅ Use a loading flag to prevent overlapping calls. - ❌ Not resetting the list on refresh – New data will be appended instead of replaced. ✅ Clear the list and reset the page counter.
- ❌ Forgetting to dispose
ScrollController– Causes memory leaks. ✅ Always dispose indisposemethod.
FAQ
- Q: How do I implement pagination with a
GridView?
A: Same principle – use aGridView.builderwith the sameScrollControllerand item count logic. - Q: How to handle pagination when the API uses page size and offset?
A: Maintainpageandlimitvariables; when fetching, passpageandlimit. Increment page after successful fetch. - Q: Can I use
GetXwidgets likeObxwith pagination?
A: Yes,Obxworks perfectly. However, be careful with large lists; useListView.builderto avoid building all items at once. - Q: How do I show a loading indicator at the bottom without affecting the scroll position?
A: Add an extra item at the end of the list that shows the loading indicator only whenisLoadingMoreis true. - Q: How to implement pagination with a search filter?
A: Reset the pagination state (page = 1, clear list) when the filter changes, then fetch new data.
Conclusion
Pagination with GetX is straightforward. By combining reactive state, scroll controllers, and proper state management, you can create smooth infinite scrolling experiences. Remember to handle loading states, errors, and edge cases to keep your app robust.