download method

void download({
  1. required String savePath,
  2. ProgressCallback? onReceiveProgress,
  3. Success? success,
  4. Failure? failure,
  5. Completed? completed,
})

Implementation

void download({
  required String savePath,
  ProgressCallback? onReceiveProgress,
  Success? success,
  Failure? failure,
  Completed? completed,
}) async {
  if (!(await _checkNetWork())) {
    return;
  }
  String url = _path.toString();
  if (isRestfulUrl()) {
    url = NetUtils.restfulUrl(_path.toString(), _params);
  }
  try {
    _options?.method = _httpType.name;
    if (_headers.isNotEmpty) {
      _options?.headers = _headers;
    }
    if (_enableGlobalHeader) {
      _options?.headers ??= {};
      _options?.headers?.addAll(_rxNet.getHeaders());
    }
    if (_toFormData) {
      _bodyData = FormData.fromMap(_params);
    }
    if (_toBodyData) {
      _bodyData = _params;
    }

    final response = await _rxNet.client!.download(url, savePath,
        onReceiveProgress: (received, total) {
      if (total != -1) {
        onReceiveProgress?.call(received, total);
      }
      if (received >= total) {
        success?.call(savePath, SourcesType.net);
      }},
        queryParameters: (isRestfulUrl() || _toFormData || _toBodyData) ? {} : _params,
        data: _bodyData,
        options: _options,
        cancelToken: _cancelToken);

    onResponse?.call(response);
    if (response.statusCode != 200) {
      failure?.call(response.data);
    }
  } catch (e) {
    failure?.call(e);
  }
  completed?.call();
}