fetchNextPage method
Fetches the next page of data if available.
Implementation
Future<void> fetchNextPage() async {
if (_fetcher == null ||
_getNextPageParam == null ||
_pages.isEmpty ||
value.isFetchingNextPage ||
!value.hasNextPage) {
return;
}
final nextParam = _getNextPageParam!(_pages.last, _pages);
if (nextParam == null) {
value = value.copyWith(hasNextPage: false);
return;
}
value = value.copyWith(isFetchingNextPage: true);
try {
final nextPage = await _fetcher!(nextParam);
if (_isDisposed) return;
_pages.add(nextPage);
final newNextParam = _getNextPageParam!(nextPage, _pages);
value = value.copyWith(
data: List.from(_pages),
isFetchingNextPage: false,
hasNextPage: newNextParam != null,
dataUpdatedAt: DateTime.now(),
);
} catch (e) {
if (_isDisposed) return;
value = value.copyWith(isFetchingNextPage: false, error: e);
}
}