request<T> method

void request<T>({
  1. required Future<BaseResp<T>> future,
  2. bool isNeedLoading = true,
  3. bool isLoadingCancelable = false,
  4. String? loadingTxt,
  5. bool isShowErrorToast = false,
  6. bool isShowErrorDetailToast = false,
  7. OnSuccess<T>? onSuccess,
  8. OnFailed? onFailed,
})

同步回调的网络请求

Implementation

void request<T>(
    {required Future<BaseResp<T>> future,
    bool isNeedLoading = true,
    bool isLoadingCancelable = false,
    String? loadingTxt,
    bool isShowErrorToast = false,
    bool isShowErrorDetailToast = false,
    OnSuccess<T>? onSuccess,
    OnFailed? onFailed}) {
  // 根据需要展示loading弹框
  if (isNeedLoading) {
    showLoading(loadingTxt: loadingTxt, cancelable: isLoadingCancelable);
  }

  future.then(
      (resp) => {
            _checkSuccessFailed(resp, (T? data) {
              // 接口请求成功
              if (!isFinishing()) {
                if (isNeedLoading) {
                  dismissLoading();
                }
                if (onSuccess != null) {
                  onSuccess(data);
                }
              }
            }, (e) {
              // 接口请求失败
              if (!isFinishing()) {
                if (isNeedLoading) {
                  dismissLoading();
                }

                if (isShowErrorDetailToast == true) {
                  showToast(e.detailMessage);
                } else if (isShowErrorToast == true) {
                  showToast(e.message);
                }
                _onFailed(onFailed, e);
              }
            })
          }, onError: (e) {
    _onFailed(onFailed, CstException.buildException(e));
  }).catchError((e) {
    return e;
  });
}