when method
Builds UI declaratively based on the infinite query state.
The data builder receives:
pages— all loaded pages as a flatList<T>hasNextPage— whether more pages are availablefetchNextPage— 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,
);
}