handlePageRequest method

Future<void> handlePageRequest(
  1. int pageKey
)

Handles pagination requests by fetching data for the specified page.

This method fetches both the list of items and the total count, then updates the paging controller accordingly. It also handles errors, including 403 Forbidden responses.

Parameters:

  • pageKey: The page key (typically the skip/offset value) to fetch.

Implementation

Future<void> handlePageRequest(int pageKey) async {
  filter.skip = pageKey;

  await Future.wait([
    repository.list(filter),
    repository.count(filter),
  ]).then((values) {
    final list = values[0] as List<T>;
    final count = values[1] as int;

    if (mounted) {
      setState(() {
        total = count;

        if (pagingController.isLastPage(filter, list, count)) {
          pagingController.appendLastPage(list);
          return;
        }

        pagingController.appendPage(list, filter.skip + list.length);
      });
    }
  }).catchError((error) {
    debugPrint('Có lỗi xảy ra');

    if (mounted) {
      pagingController.error(error);
      if (error is DioException) {
        if (error.response?.statusCode == 403) {
          setState(() {
            isForbidden = true;
          });
          return;
        }
      }
    }
  });
}