getFileFromUrl method

Future<List<File?>> getFileFromUrl({
  1. required String source,
})

Downloads file from the given source URL and saves it to a temporary directory on the device.

Returns a list of Files containing the downloaded file.

Example usage:

List<File?> files = await RhUtils.instance.getFileFromUrl(source: 'https://example.com/image.jpg');

Implementation

Future<List<File?>> getFileFromUrl({required String source}) async {
  List<File?> files = [];

  Directory tempDir = await getTemporaryDirectory();
  String tempPath = tempDir.path;
  File file = File('$tempPath${Random().nextInt(100)}.png');
  http.Response response = await http.get(Uri.parse(source));
  await file.writeAsBytes(response.bodyBytes);

  files.add(file);
  return files;
}