Skip to content

Infinite Queries ​

InfiniteQueryObserver manages paginated data where pages are fetched sequentially, either forward (e.g. infinite scrolling) or backward (e.g. chat message histories).

Data is encapsulated in an InfiniteQueryData<TData, TPageParam>, which stores:

  • pages: A List<TData> containing each loaded page.
  • pageParams: A List<TPageParam> containing the parameter used for each page.

Setup Example ​

dart
final observer = InfiniteQueryObserver<PageResult, Exception, int>(
  cache: cache,
  queryKey: QueryKey(['feed']),
  initialPageParam: 1,
  queryFn: (page) => api.fetchFeedPage(page),
  getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) {
    return lastPage.hasMore ? lastPageParam + 1 : null;
  },
  getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) {
    return firstPageParam > 1 ? firstPageParam - 1 : null;
  },
  maxPages: 10, // Retains at most 10 pages in memory
);

observer.initialize();

Pagination Actions ​

Fetching Next Page ​

dart
if (observer.hasNextPage && !observer.query.isFetching) {
  observer.fetchNextPage();
}

Fetching Previous Page ​

dart
if (observer.hasPreviousPage && !observer.query.isFetching) {
  observer.fetchPreviousPage();
}

Sequential Refetching ​

When an infinite query is refetched (or invalidated), InfiniteQueryObserver refetches all existing cached pages sequentially, preserving the exact page count and updating cached entries in-place.

If any page fetch fails during the sequence, the refetch stops, preserving previously resolved pages and safely reporting the error without data corruption.

Released under the MIT License.