Module 2
Topic 6

Pagination

Efficiently loading and displaying large datasets β€” from infinite scrolling to page-based navigation.

Flutter Cookbook
What Is Pagination?

Pagination is the process of dividing a large dataset into smaller chunks (pages) that are loaded incrementally. It's essential for:

  • Performance – Loading all data at once can be slow and memory-intensive
  • UX – Users don't want to wait for thousands of items to load
  • Bandwidth – Reduces data usage by loading only what's needed
  • Scalability – Works with APIs that have rate limits

🎯 The Problem Pagination Solves

Imagine an API that returns 10,000 users. Loading all of them at once would take several seconds, use lots of memory, and provide a poor user experience. Pagination solves this by loading 20 users at a time , with the option to load more when needed.

Pagination Strategies

There are three main pagination strategies used in Flutter apps:

♾️
Infinite Scroll
Load more data as the user scrolls down
πŸ“„
Page-Based
Navigate between pages (Next/Previous buttons)
πŸ”’
Load More
Explicit "Load More" button at the bottom
API Pagination Patterns

Different APIs use different pagination patterns. Here are the most common:

Implementing Infinite Scroll

Infinite scroll is the most common pagination pattern in mobile apps. Here's how to implement it using a ScrollController and ListView.builder .

Step-by-Step Explanation
1.
Create the API Service – Define a service that fetches pages with page and limit parameters.
2.
Set up state variables – Track _items , _currentPage , _isLoading , and _hasMore .
3.
Add ScrollController – Listen for scroll events to detect when the user reaches the bottom.
4.
Implement load more logic – When the user scrolls to the bottom (within 200px), fetch the next page.
5.
Show loading indicator – Display a progress indicator at the bottom while loading more items.
6.
Handle errors – Show error messages and provide a retry option.
Page-Based Pagination

Page-based pagination uses buttons to navigate between pages. This is common in web apps and some mobile apps.

Load More Button Pattern

The "Load More" button gives users explicit control over when to load more data. This is great for users with limited data plans or slow connections.

Pagination Best Practices

βœ… Do's

  • Always show a loading indicator when fetching more data
  • Handle and display errors with retry options
  • Use a reasonable page size (10-50 items per page)
  • Cache data locally to reduce API calls
  • Use ScrollController for infinite scroll detection
  • Prevent duplicate requests with loading state

❌ Don'ts

  • Don't fetch the same page multiple times
  • Don't ignore the loading state (avoid duplicate requests)
  • Don't make the page size too large (hurts performance)
  • Don't forget to handle the "no more items" state
  • Don't show loading indicators for empty pages
Common Mistakes
❌ Mistake 1: Not preventing duplicate requests

Without a loading state, users can trigger multiple requests by scrolling quickly or clicking "Load More" multiple times.

βœ… Correct: Use a loading flag

Track _isLoading and check it before making any new request.

❌ Mistake 2: Not handling "no more items"

If you don't indicate that there are no more items, users will keep trying to load more.

βœ… Correct: Show "No more items" message

When _hasMore is false, show a message instead of a loading indicator.

❌ Mistake 3: Hardcoding page size in multiple places

If the page size is used in multiple files, changing it becomes tedious and error-prone.

βœ… Correct: Define page size as a constant

Use a static constant like static const int pageSize = 20; in your service class.

🎯 Key Takeaway

Pagination is essential for handling large datasets efficiently. Infinite scroll provides the best UX for mobile apps. Always handle loading states , errors , and the "no more items" condition. Never let users trigger duplicate requests.