loadPage method

Future loadPage({
  1. PageKeyType? nextPageKey,
  2. bool isRefresh = false,
})

Implementation

Future loadPage({PageKeyType? nextPageKey, bool isRefresh = false}) async {
  var items =
      _pagingState.maybeMap((value) => value.items, orElse: () => null);
  await dataSource.loadPage(isRefresh: isRefresh, newKey: nextPageKey).then(
      (value) {
    int? itemCount = isRefresh
        ? [...value].length
        : items != null
            ? [...items, ...value].length
            : [...value].length;

    bool hasNextPage = dataSource.currentKey != null && !dataSource.isEndList;

    bool hasItems = itemCount > 0;

    bool isListingUnfinished = hasItems && hasNextPage;

    bool isOngoing = isListingUnfinished;

    bool isCompleted = hasItems && !hasNextPage;

    /// The current pagination status.
    PagingStatus status =
        (isOngoing) ? PagingStatus.ongoing : PagingStatus.completed;

    emit(PagingState<PageKeyType, ItemType>(
        isRefresh
            ? [...value]
            : items != null
                ? [...items, ...value]
                : [...value],
        status,
        false));
  }, onError: (e) {
    if (dataSource.currentKey == null) {
      emit(PagingState<PageKeyType, ItemType>.error(e));
    } else {
      _pagingState.maybeMap(
          (value) => emit(PagingState<PageKeyType, ItemType>(
              value.items, PagingStatus.noItemsFound, true)),
          orElse: () => null);
    }
  });
}