requestList method

Future<void> requestList(
  1. Method method,
  2. String path, {
  3. String? baseUrl,
  4. Map<String, dynamic>? params,
  5. Map<String, dynamic>? header,
  6. dynamic data,
  7. CacheList? cacheList,
  8. bool isCache = false,
  9. DioCacheConfig? dioCacheConfig,
  10. SuccessListDy? successList,
  11. Empty? empty,
  12. Error? error,
})

请求,返回参数为 List method:请求方法,NWMethod.POST等 path:请求地址 params:请求参数 data:post请求data success:请求成功回调 error:请求失败回调

Implementation

Future<void> requestList(Method method, String path,
    {String? baseUrl,
    Map<String, dynamic>? params,
    Map<String, dynamic>? header,
    dynamic data,
    CacheList? cacheList,
    bool isCache = false,
    DioCacheConfig? dioCacheConfig,
    SuccessListDy? successList,
    Empty? empty,
    Error? error}) async {
  try {
    _setBaseUrl(baseUrl);
    _changeHeader(header);

    if (headerDelegate != null) {
      headerDelegate?.call(params);
      updateHeader();
    }
    dioCacheConfig ??= cacheConfig;
    Response? response = await _dio?.request(path,
        queryParameters: params,
        data: data,
        options: dioCacheConfig != null
            ? buildCacheOptions(dioCacheConfig.maxAge,
                maxStale: dioCacheConfig.maxStale,
                primaryKey: dioCacheConfig.primaryKey,
                subKey: dioCacheConfig.subKey,
                forceRefresh: dioCacheConfig.forceRefresh,
                options: Options(method: methodValues[method]))
            : isCache
                ? buildCacheOptions(const Duration(days: 7),
                    options: Options(method: methodValues[method]))
                : Options(method: methodValues[method]));
    if (response != null) {
      BaseListRes entity = BaseListRes.fromJson(response.data);
      // 本公司接口的code = 200、0、1 都认为请求成功
      // 注:请求接口为数组格式一般是分页接口,目前无法根据接口对空数据格式进行判断
      // 所以这里空数据需要自己在success回调里自己判断
      if (isRequestSuccess(entity.code)) {
        if (!isCache && dioCacheConfig == null) {
          successList?.call(entity.data);
        } else {
          successList?.call(entity.data);
          cacheList?.call(entity.data);
        }
      } else {
        error?.call(ErrorRes(code: entity.code, message: entity.message));
      }
    } else {
      error?.call(ErrorRes(code: -1, message: "未知错误"));
    }
  } on DioError catch (e) {
    error?.call(createErrorEntity(e));
  }
}