initData method

void initData({
  1. bool? isPageInit,
})

Implementation

void initData({bool? isPageInit}) async {
  final pageProviderKey = _pagingBuilderController.getProviderKey();
  // This logic only needs to be called during the initialization of the page.
  if (isPageInit ?? true) {
    if (widget.isPersistent) {
      ref.read(pagingControllerProvider(pageProviderKey).notifier).keepAlive();
    }
    var cacheData = widget.pagingDataController.getInitData(ref);
    if (cacheData != null && cacheData.isNotEmpty && widget.forceUseInitData) {
      ref.read(pagingControllerProvider(pageProviderKey).notifier).appendRefreshPage(
            cacheData,
            ref.read(pagingControllerProvider(pageProviderKey).notifier).arg,
          );
    }
  }

  try {
    // If it’s the page initialization, use the default request parameters; otherwise, use the parameters passed during the refresh.
    var firstPageData = await widget.pagingDataController.requestData((isPageInit ?? false)
        ? widget.pagingDataController.getFirstDataPageKey()
        : ref.read(pagingControllerProvider(pageProviderKey)).nextPageKey);

    if (!mounted) return;

    if (firstPageData.error != null) {
      ref.read(pagingControllerProvider(pageProviderKey).notifier).loadError(firstPageData.error);
    } else {
      ref
          .read(pagingControllerProvider(pageProviderKey).notifier)
          .appendRefreshPage(firstPageData.itemList as List<T>, firstPageData.nextPageKey);
    }
  } catch (e) {
    ref.read(pagingControllerProvider(pageProviderKey).notifier).loadError(e);
  }
}