fetch method
Fetches the initial page of data.
Implementation
Future<void> fetch({
required Future<T> Function(TPageParam?) fetcher,
required TPageParam? Function(T lastPage, List<T> allPages)
getNextPageParam,
}) async {
_fetcher = fetcher;
_getNextPageParam = getNextPageParam;
if (_pages.isNotEmpty) return;
value = value.copyWith(status: QueryStatus.fetching, isFetching: true);
try {
final firstPage = await fetcher(null);
if (_isDisposed) return;
_pages = [firstPage];
final nextParam = getNextPageParam(firstPage, _pages);
value = value.copyWith(
status: QueryStatus.success,
data: List.from(_pages),
isFetching: false,
dataUpdatedAt: DateTime.now(),
hasNextPage: nextParam != null,
);
} catch (e) {
if (_isDisposed) return;
value = value.copyWith(
status: QueryStatus.error, error: e, isFetching: false);
}
}