krequest<T> function

Future<void> krequest<T>(
  1. Future<Response> request(), {
  2. dynamic onSuccess(
    1. T?
    )?,
  3. dynamic onFail(
    1. String?,
    2. String?,
    3. Exception
    )?,
})

请求数据,,返回data数据为原始数据类型

    request<String>(
      () => ApiRepository.get.nearbyMaxsShop(1, 0, 0),
      onSuccess: <String>(data) {},
    );

    request<int>(
      () => ApiRepository.get.nearbyMaxsShop(1, 0, 0),
      onSuccess: <int>(data) {},
    );

Implementation

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