fetch method

Future<void> fetch({
  1. bool isLastPage = false,
  2. bool isFetchFirstPage = false,
  3. DyFetchCustomParam? customParameter,
})

This function is for fetch data for first page

This function will reset state page to initial state

Implementation

Future<void> fetch({
  /// If this enabled, indicator 'hasNextPage' will set false
  bool isLastPage = false,

  /// If this enabled, will fetch first page on the list
  bool isFetchFirstPage = false,

  /// Custom paramter
  DyFetchCustomParam? customParameter,
}) async {
  bool isFirstPage = page == pageStart;
  if (isFetchFirstPage) isFirstPage = true;

  if (isFirstPage) {
    _setInLoading();
    try {
      final list = await _fetchData(
          DyFetchParam(
            page: pageStart,
          ),
          customParameter);
      _addItemList(
        list,
        currentPage: pageStart,
        isLastPage: isLastPage,
      );
      return;
    } catch (e) {
      error = e;
      isLoading = false;
      _updateState();
    }
    return;
  }

  //old
  // if (!hasNextPage) {
  //   isLoadingNextPage = false;
  //   _updateState();
  // }
  // if (isLoadingNextPage) return;

  if (!hasNextPage || isLoadingNextPage) return;

  int currentPage = page;
  bool isCurrentLastPage = isLastPage;
  isLoadingNextPage = true;
  _updateState();
  try {
    final list = await _fetchData(
        DyFetchParam(
          page: currentPage,
        ),
        customParameter);
    isLoadingNextPage = false;

    if (currentPage != page && pageStart == page) {
      return;
    }
    _addItemList(
      list,
      currentPage: currentPage,
      isLastPage: isCurrentLastPage,
    );
  } catch (e) {
    isLoadingNextPage = false;
    error = e;
    _updateState();
  }
}