requestList<T> function

Future<void> requestList<T>(
  1. Future<Response> request(),
  2. T fromJsonT(
    1. dynamic json
    ), {
  3. dynamic onSuccess(
    1. List<T>?
    )?,
  4. dynamic onFail(
    1. String?,
    2. String?
    )?,
})

请求数据,返回集合

    requestList<ShopDto>(
      () => ApiRepository.get.nearbyMaxsShop(1, 0, 0),
      (json) => ShopDto.fromJson(json),
      onSuccess: <ShopDto>(data) {},
    );

Implementation

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