download method
void
download({
- required String savePath,
- ProgressCallback? onReceiveProgress,
- Success? success,
- Failure? failure,
- Completed? completed,
下载文件
Implementation
void download({
required String savePath,
adapter.ProgressCallback? onReceiveProgress,
Success? success,
Failure? failure,
Completed? completed,
}) async {
if (!(await _checkNetWork())) {
return;
}
final url = _buildFinalUrl();
final payload = _resolveRequestPayload();
final file = File(savePath);
try {
if (!file.parent.existsSync()) {
file.parent.createSync(recursive: true);
}
// 构建 AdapterRequest
final adapterRequest = _buildAdapterRequest(
url: url,
queryParams: payload.queryParams,
data: payload.body,
contentType: payload.contentType,
);
// 使用适配器下载文件
final adapter = _requireAdapter();
final response = await adapter.download(
adapterRequest,
savePath,
onProgress: (received, total) {
onReceiveProgress?.call(received, total > 0 ? total : received);
},
);
onResponse?.call(response);
if (response.isSuccess) {
success?.call(savePath, SourcesType.net);
} else {
failure?.call(response.data);
}
} on AdapterException catch (e) {
failure?.call(e);
} catch (e) {
failure?.call(e);
} finally {
completed?.call();
}
}