downloadFile method

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

Downloads the content from the specified URL and saves it to a file.

Parameters:

  • url: The URL from which to download the content.
  • savePath: The directory path where the file should be saved.
  • filename: The name of the file to be saved.

Returns:

  • A Future that resolves to the io.File if the download is successful; otherwise, null.

Implementation

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