downloadFile function

Future<String?> downloadFile(
  1. Uri uri,
  2. String downloadFolder,
  3. String fileName
)

Implementation

Future<String?> downloadFile(
    Uri uri, String downloadFolder, String fileName) async {
  final client = HttpClient();
  client.userAgent = "auto_update";
  final request = await client.getUrl(uri);
  final response = await request.close();

  File file = File(downloadFolder + fileName);

  if (await file.exists()) {
    await file.delete();
  }

  await file.create();

  if (response.statusCode == 200) {
    await response.pipe(file.openWrite());
    return file.absolute.path;
  }
}