arch-atlas

Pagination part 1

Presentation (Integration)

09:00

Summary

This lesson introduces the core concept of pagination by exploring how the Google Books API handles bulk data retrieval. The instructor explains the performance and bandwidth reasons for avoiding fetching all records simultaneously, demonstrating how the API limits responses using the maxResults and startIndex query parameters in Postman. While no major Flutter implementation occurs in this video besides a minor bug fix, the conceptual groundwork is laid for adding infinite scrolling capabilities to the application's UI.

Key Ideas

  • Setting itemCount on a ListView.builder is strictly necessary to prevent out-of-bounds exceptions when iterating over finite lists.
  • Pagination protects both the client and server from high load by delivering massive datasets in smaller, sequential chunks.
  • The Google Books API defaults to returning 10 items per request, controllable via the maxResults parameter (maximum 40).
  • The API utilizes an offset-based pagination strategy using the startIndex parameter instead of a standard pageNumber approach.
  • Fetching subsequent batches of 10 requires incrementing the startIndex by 10 for each request (e.g., 0, 10, 20, 30).

Code Snippets

Adding itemCount to the FeaturedBooksListView to fix infinite scroll exceptiondart
import '../../../../domain/entities/book_entity.dart';
import 'custom_book_item.dart';
import 'package:flutter/material.dart';

class FeaturedBooksListView extends StatelessWidget {
  const FeaturedBooksListView({Key? key, required this.books})
      : super(key: key);

  final List<BookEntity> books;
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: MediaQuery.of(context).size.height * .3,
      child: ListView.builder(
        itemCount: books.length,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) {
          return Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: CustomBookImage(
              imageUrl: books[index].image ?? '',
            ),
          );
        },
      ),
    );
  }
}

Architecture Insight

Architecture insight

Pagination is the case where scroll position genuinely is business state: the current page index, whether there's a "load more" in flight, whether the last page has been reached — all of that has to live in the Cubit because the widget tree can't remember it across rebuilds.

The trick is keeping the scroll *position* (a pixel offset) out of the Cubit while keeping the page *index* (a count of loaded pages) inside it. The first is a presentation detail the widget owns; the second is a fact about the data the Cubit has to track.

Looking ahead — pagination-states-part-1 formalizes this with explicit PaginationLoading / PaginationFailure / PaginationSuccess substates so the UI can show 'loading more' without replacing the existing list.

My Notes

This lesson serves primarily as a theoretical primer and API exploration using Postman. The instructor addressed a common UI bug by adding itemCount: books.length to the ListView.builder, fixing the crash caused by Flutter attempting to build infinite list items from a finite array. [Editor's note] The instructor highlights the difference between offset-based pagination (startIndex) and traditional page-based pagination. In a strict Clean Architecture setup, this API-specific quirk should be abstracted away inside the Data layer (specifically within the DataSource or Repository). The Domain layer should ideally just request "Page 2", and the Data layer should be responsible for translating that into startIndex = 10. It will be interesting to see how this abstraction is handled when we implement the Dart code in the upcoming lessons.