fetchNewPage method

Future<void> fetchNewPage()

Fetches a new page by calling pageFuture

Implementation

Future<void> fetchNewPage() async {
  if (!this._isFetching) {
    this._isFetching = true;

    List<T> page;
    try {
      page = await this.pageFuture!(this._numberOfLoadedPages);
      this._numberOfLoadedPages++;
    } catch (error) {
      this._error = error;
      this._isFetching = false;
      this.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 > this.pageSize!) {
      this._isFetching = false;
      throw ('Page length ($length) is greater than the maximum size (${this.pageSize})');
    }

    if (length > 0 && length < this.pageSize!) {
      // 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
      this._appendedItems = List.generate(this.pageSize! - length, (_) => {});
    }

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