request function
发送请求并解析远程服务器返回的result对应的实体类型
baseUrl: 主机地址 path: 请求路径 data: 请求参数 queryParameters: URL携带请求参数 connectTimeout: 超时时间 validResult: 是否检验返回结果 autoLoading: 展示Loading
Implementation
Future<Result> request(
{String? baseUrl,
String path = '',
data,
Map<String, dynamic>? queryParameters,
BaseCancelToken? cancelToken,
Options? options,
Duration? connectTimeout,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
bool validResult = true,
bool autoLoading = false}) async {
if (autoLoading) {
showLoading();
}
Session session = Session(
config: Config(
baseUrl:
baseUrl?.isNotEmpty == true ? baseUrl! : _session.config.baseUrl,
createHttpClient: _session.config.createHttpClient,
badCertificateCallback: _session.config.badCertificateCallback,
connectTimeout: _session.config.connectTimeout,
receiveTimeout: _session.config.receiveTimeout,
code: _session.config.code,
data: _session.config.data,
list: _session.config.list,
message: _session.config.message,
validCode: _session.config.validCode,
errorTimeout: _session.config.errorTimeout,
errorConnection: _session.config.errorConnection,
errorBadResponse: _session.config.errorBadResponse,
errorCancel: _session.config.errorCancel,
errorUnknown: _session.config.errorUnknown),
onRequest: _session.onRequest,
onResult: validResult ? _session.onResult : null);
var extra;
if (_onRequest != null) {
extra = await _onRequest!(RequestOptions(
baseUrl: session.config.baseUrl,
path: path,
data: data,
method: options?.method,
headers: options?.headers,
extra: options?.extra,
responseType: options?.responseType,
validateStatus: options?.validateStatus,
contentType: options?.contentType,
receiveDataWhenStatusError: options?.receiveDataWhenStatusError,
followRedirects: options?.followRedirects,
maxRedirects: options?.maxRedirects,
persistentConnection: options?.persistentConnection,
requestEncoder: options?.requestEncoder,
responseDecoder: options?.responseDecoder,
listFormat: options?.listFormat,
queryParameters: queryParameters,
cancelToken: cancelToken,
connectTimeout: connectTimeout,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
));
}
Result result = await session.request(
path,
data: data,
queryParameters: queryParameters,
cancelToken: cancelToken,
options: options,
connectTimeout: connectTimeout,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
if (autoLoading) {
dismissLoading();
}
if (_onResult != null) {
return _onResult!(result, validResult, extra);
}
return result;
}