request<T> method
Future<void>
request<T>(
- Method method,
- String path, {
- String? baseUrl,
- Map<
String, dynamic> ? params, - Map<
String, dynamic> ? header, - dynamic data,
- ProgressCallback? onSendProgress,
- ProgressCallback? onReceiveProgress,
- SuccessDy? success,
- Empty? empty,
- Cache? cache,
- bool isCache = false,
- DioCacheConfig? dioCacheConfig,
- Error? error,
请求,返回参数为 T method:请求方法,NWMethod.GET等 path:请求地址 params:请求参数 success:请求成功回调 error:请求失败回调
Implementation
Future<void> request<T>(Method method, String path,
{String? baseUrl,
Map<String, dynamic>? params,
Map<String, dynamic>? header,
data,
ProgressCallback? onSendProgress, // 上传数据进度
ProgressCallback? onReceiveProgress, // 接受数据进度
SuccessDy? success,
Empty? empty,
Cache? cache,
bool isCache = false,
DioCacheConfig? dioCacheConfig,
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,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
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]),
);
print('isCache $isCache');
if (response != null) {
BaseRes entity = BaseRes.fromJson(response.data);
// 本公司接口的code = 200、0、1 都认为请求成功
if (isRequestSuccess(entity.code)) {
// code 请求成功
if (entity.data != null && entity.data != '') {
// 有数据 未开启缓存
if (!isCache && dioCacheConfig == null) {
success?.call(entity.data);
} else {
success?.call(entity.data);
cache?.call(entity.data);
}
} else {
empty?.call();
}
} 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));
}
}