requestWithState<T> method

void requestWithState<T>({
  1. required Future<BaseResp<T>> future,
  2. String? loadingTxt,
  3. bool isShowErrorDetail = false,
  4. String? retryTxt,
  5. OnSuccess<T>? onSuccess,
  6. OnFailed? onFailed,
})

结合多状态UI的网络请求

Implementation

void requestWithState<T>({
  required Future<BaseResp<T>> future,
  String? loadingTxt,
  bool isShowErrorDetail = false,
  String? retryTxt,
  OnSuccess<T>? onSuccess,
  OnFailed? onFailed,
}) {
  // 展示loading状态UI
  showLoadingState(loadingTxt: loadingTxt);

  super.request(
      future: future,
      isNeedLoading: false,
      loadingTxt: loadingTxt,
      isShowErrorToast: false,
      isShowErrorDetailToast: false,
      onSuccess: (T? data) {
        if (!isFinishing()) {
          // 接口成功,展示内容状态UI
          showContentState();

          if (onSuccess != null) {
            // 成功回调,可以在此回调自定义操作
            onSuccess(data);
          }
        }
      },
      onFailed: (CstException e) {
        if (!isFinishing()) {
          // 接口失败,展示错误状态UI
          showErrorState(
              errorTxt: isShowErrorDetail ? e.detailMessage : e.message,
              retryTxt: retryTxt);

          if (onFailed != null) {
            // 失败回调,可以在此回调自定义操作
            onFailed(e);
          }
        }
      });
}