fetchNextPage method
Fetch the next page of data.
If already fetching or no next page exists, this does nothing.
Implementation
Future<void> fetchNextPage() async {
if (isFetchingNextPage.value || !hasNextPage.value || isDisposed) {
return;
}
isFetchingNextPage.value = true;
update(); // Update UI to show loading footer
try {
// 1. Fetch the new page using the stored param
final newPage = await infiniteFetcher(_nextPageParam);
if (isDisposed) return;
// 2. Append to existing pages
final currentPages = data.value ?? [];
final allPages = [...currentPages, newPage];
// 3. Update the reactive data
// We use setData to ensure timestamps are updated in the cache
data.value = allPages;
// Manually update cache entry since we modified data outside standard fetch()
ZenQueryCache.instance.updateCache(queryKey, allPages, DateTime.now());
// 4. Calculate the cursor for the NEXT fetch
final nextParam = getNextPageParam(newPage, allPages);
_nextPageParam = nextParam;
hasNextPage.value = nextParam != null;
// 5. Ensure status is success
if (status.value != ZenQueryStatus.success) {
status.value = ZenQueryStatus.success;
}
// Clear any previous errors since this succeeded
error.value = null;
} catch (e) {
if (!isDisposed) {
// We generally set the error state but keep the old data
error.value = e;
// Note: We do NOT set status to ZenQueryStatus.error because
// we still have valid pages displayed.
// The UI should check 'error' to show a toast/snackbar.
}
} finally {
if (!isDisposed) {
isFetchingNextPage.value = false;
update();
}
}
}