request<T> method

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

同步回调的网络请求

Implementation

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

  if (isNeedDialogLoading) {
    AppHubUtil.showLoading();
  }

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

              if (isShowErrorDetailToast == true) {
                showToast(e.detailMessage);
              } else if (isShowErrorToast == true) {
                showToast(e.message);
              }

              if (isNeedLoading && viewState != ViewState.error) {
                showError(e.message ?? '');
              }
              _onFailed(onFailed, e);
            }
          },
        ),
        onError: (e) {
          if (isNeedLoading && viewState != ViewState.error) {
            showError(e.message ?? '');
          }
          _onFailed(onFailed, CstException.buildException(e));
        },
      )
      .catchError((e) {
        return e;
      });
}