downloadFile method

Future<File?> downloadFile({
  1. required String url,
  2. required String savePath,
  3. required String filename,
})

Downloads content directly to a file on the local file system.

Performs a GET request and saves the response directly to disk without loading the entire content into memory. Ideal for large files.

Parameters:

  • url: The URL to download content from (can be absolute or relative to baseUrl)
  • savePath: Directory path where the file should be saved
  • filename: Name of the file to create

Returns:

  • File instance if download succeeds (status 200)
  • null if download fails or status is not 200

Example:

final file = await client.downloadFile(
  url: '/files/large-document.pdf',
  savePath: '/Downloads',
  filename: 'document.pdf',
);
if (file != null) {
  print('Downloaded to: ${file.path}');
}

Implementation

Future<io.File?> downloadFile({
  required String url,
  required String savePath,
  required String filename,
}) async {
  final filePath = join(savePath, filename);
  try {
    final response = await dio.download(url, filePath);
    return response.statusCode == 200 ? io.File(filePath) : null;
  } catch (e) {
    return null;
  }
}