when method

Widget when({
  1. required Widget data(
    1. List<T> pages,
    2. bool hasNextPage,
    3. VoidCallback fetchNextPage
    ),
  2. Widget loading()?,
  3. Widget error(
    1. Object error,
    2. VoidCallback retry
    )?,
  4. Widget idle()?,
  5. bool autoFetch = true,
  6. bool showStaleData = true,
})

Builds UI declaratively based on the infinite query state.

The data builder receives:

  • pages — all loaded pages as a flat List<T>
  • hasNextPage — whether more pages are available
  • fetchNextPage — call to load the next page

Example:

postsQuery.when(
  data: (pages, hasNextPage, fetchNextPage) => ListView.builder(
    itemCount: pages.length + (hasNextPage ? 1 : 0),
    itemBuilder: (context, index) {
      if (index == pages.length) {
        fetchNextPage();
        return const CircularProgressIndicator();
      }
      return PostTile(pages[index]);
    },
  ),
  loading: () => const CircularProgressIndicator(),
  error: (e, retry) => ErrorView(e, onRetry: retry),
)

Implementation

Widget when({
  required Widget Function(
    List<T> pages,
    bool hasNextPage,
    VoidCallback fetchNextPage,
  ) data,
  Widget Function()? loading,
  Widget Function(Object error, VoidCallback retry)? error,
  Widget Function()? idle,
  bool autoFetch = true,
  bool showStaleData = true,
}) {
  return ZenQueryBuilder<List<T>>(
    query: this,
    builder: (context, pages) => data(
      pages,
      hasNextPage.value,
      () => fetchNextPage(),
    ),
    loading: loading,
    error: error,
    idle: idle,
    autoFetch: autoFetch,
    showStaleData: showStaleData,
  );
}