fetchNewPage method

Future<void> fetchNewPage()

Fetches a new page by calling pageFuture

Implementation

Future<void> fetchNewPage() async {
  if (!_isFetchingNotifier.value) {
    _isFetchingNotifier.value = true;

    List<T> page = [];
    try {
      page = await pageFuture.call(_numberOfLoadedPages);
      _numberOfLoadedPages++;
    } catch (error) {
      _error = error;
      _isFetchingNotifier.value = false;
      notifyListeners();
      return;
    }

    // Get length accounting for possible null Future return. We'l treat a null Future as an empty return
    final int length = page.length;

    if (length > (pageSize ?? 0)) {
      _isFetchingNotifier.value = false;
      _hasMoreItems = false;
      FlutterError.dumpErrorToConsole(
          FlutterErrorDetails(exception: Exception('''
            Inconsistent page length and page size.
            Page length ($length) is greater than the maximum size ($pageSize).
            This is not allowed in pagination and therefore pagination will stop inmediately
            ''')),
          forceReport: true);
//        throw ('Page length ($length) is greater than the maximum size (${this.pageSize})');
    }

    if (length > 0 && length < (pageSize ?? 0)) {
      // This should only happen when loading the last page.
      // In that case, we append the last page with a few items to make its size
      // similar to normal pages. This is useful especially with GridView,
      // because we want the loading to show on a new line on its own
      _appendedItems = List.generate((pageSize ?? 0) - length, (_) => {});
    }

    if (length == 0) {
      _hasMoreItems = false;
    } else {
      _loadedItems.addAll(page);
    }

    _isFetchingNotifier.value = false;
    notifyListeners();
  }
}