download method

  1. @override
Future<Either<Failure, Response>> download({
  1. required String endpoint,
  2. required String savePath,
  3. Map<String, dynamic>? params,
  4. Duration? receiveTimeout,
  5. Duration? sendTimeout,
  6. Map<String, String>? headers,
  7. ProgressCallback? onReceiveProgress,
  8. CancelToken? cancelToken,
  9. bool deleteOnError = true,
  10. bool showLoader = true,
})
override

Downloads the file at endpoint and streams it directly to savePath, instead of loading the whole response into memory like get would.

Use for images, PDFs, exports, or any file response.

Example:

final dir = await getApplicationDocumentsDirectory();
final result = await _api.download(
  endpoint: '/files/report.pdf',
  savePath: '${dir.path}/report.pdf',
  onReceiveProgress: (received, total) => print('${received / total * 100}%'),
);

result.fold(
  (failure) => showError(failure.message),
  (_) => openFile(savePath),
);

Implementation

@override
Future<Either<Failure, Response>> download({
  required String endpoint,
  required String savePath,
  Map<String, dynamic>? params,
  Duration? receiveTimeout,
  Duration? sendTimeout,
  Map<String, String>? headers,
  ProgressCallback? onReceiveProgress,
  CancelToken? cancelToken,
  bool deleteOnError = true,
  bool showLoader = true,
}) {
  return _respond(HttpMethod.get, endpoint,
      data: savePath, params: params, headers: headers);
}