testPaginationController<K, T> function

PaginationController<K, T> testPaginationController<K, T>({
  1. List<T> items = const [],
  2. PaginationStatus status = PaginationStatus.loaded,
  3. K? currentPageKey,
  4. bool hasMorePages = true,
  5. Object? error,
  6. int? totalItems,
})

Creates a PaginationController pre-populated with a known state, ideal for widget tests.

No network calls are made — the controller starts with the given items, status, and currentPageKey.

final controller = testPaginationController<int, String>(
  items: ['a', 'b', 'c'],
  status: PaginationStatus.loaded,
  currentPageKey: 1,
);

Implementation

PaginationController<K, T> testPaginationController<K, T>({
  List<T> items = const [],
  PaginationStatus status = PaginationStatus.loaded,
  K? currentPageKey,
  bool hasMorePages = true,
  Object? error,
  int? totalItems,
}) {
  final controller = PaginationController<K, T>(
    fetchPage: (_) async => <T>[],
    initialPageKey: currentPageKey,
    // Provide a no-op nextPageKeyBuilder for non-int key types
    nextPageKeyBuilder: K == int ? null : (key, _) => key,
  );

  controller.value = PaginationState<K, T>(
    items: items,
    pageKey: currentPageKey,
    status: status,
    hasMorePages: hasMorePages,
    error: error,
    totalItems: totalItems,
  );

  return controller;
}