mGetPageData<T> function

Future<MIndicatorResult> mGetPageData<T>({
  1. required Future<List<T>?> reqData,
  2. required MPageState<T> mPageState,
  3. List<T>? handleData(
    1. List<T> value
    )?,
})

Implementation

Future<MIndicatorResult> mGetPageData<T>({
  required Future<List<T>?> reqData,
  required MPageState<T> mPageState,
  List<T>? Function(List<T> value)? handleData,
}) async {
  MIndicatorResult result = MIndicatorResult.success;

  try {
    List<T>? value = await reqData;
    if (value == null) {
      mPageState.error.value = true;
      result = MIndicatorResult.fail;
    } else {
      if (handleData != null) {
        value = handleData(value);
      }
      if (mPageState.goPage == 1) {
        mPageState.dataList.value = value!;
      } else {
        mPageState.dataList.addAll(value!);
      }
      if (mPageState.goPage > 1) {
        // Check if there is no more data
        if (value.length < MConfig.pageLimit) {
          result = MIndicatorResult.noMore;
        }
      }
      mPageState.error.value = false;
    }
  } catch (e, s) {
    mLogger.e('message::$e, stack::${s.toString()}');
    mPageState.error.value = true;
  } finally {
    mPageState.isLoading.value = false;
  }
  return result;
}