krequestBool function

Future<void> krequestBool(
  1. Future<Response> request(), {
  2. dynamic onSuccess(
    1. bool
    )?,
  3. dynamic onFail(
    1. String?,
    2. String?,
    3. Exception
    )?,
  4. bool isThrowEx = true,
})

请求数据,返回bool数据,接口调用成功就返回结果

    krequestBool(() => ApiRepository.get.getTemporaryCode(),
        onSuccess: (result) {});

Implementation

Future<void> krequestBool(Future<Response> Function() request,
    {Function(bool)? onSuccess,
    Function(String?, String?, Exception)? onFail,
    bool isThrowEx = true}) async {
  try {
    var response = await request();
    if (response.statusCode == HttpStatus.ok) {
      var json = response.data;
      var result = ResultBoolDto.fromJson(json);
      if (result.isSuccess()) {
        onSuccess?.call(true);
      } else {
        if (isThrowEx) {
          throw BizException(code: result.errorCode, message: result.message);
        } else {
          onSuccess?.call(false);
        }
      }
    } else {
      KHttpPlatform.get.throwHandler
          ?.handleStatusCode(response.statusCode, onFail: onFail);
    }
  } on Exception catch (ex) {
    KHttpPlatform.get.throwHandler?.handleError(ex, onFail: onFail);
  }
}