krequestFutureT<T> function

Future<T?> krequestFutureT<T>(
  1. Future<Response> request(),
  2. T fromJsonT(
    1. dynamic json
    ), {
  3. dynamic onFail(
    1. String?,
    2. String?,
    3. Exception
    )?,
})

请求数据,返回data数据为对象

  void loading() async {
    var data = await requestFutureT(
      () => ApiRepository.get.getShopDetail("1"),
      (json) => ShopDto.fromJson(json),
    );
  }

Implementation

Future<T?> krequestFutureT<T>(
    Future<Response> Function() request, T Function(dynamic json) fromJsonT,
    {Function(String?, String?, Exception)? onFail}) async {
  try {
    var response = await request();
    if (response.statusCode == HttpStatus.ok) {
      var json = response.data;
      var result = ResultDto<T>.fromJson(json, (json) => fromJsonT.call(json));
      if (result.isSuccess()) {
        return 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);
  }
  return null;
}