'Flutter 'File' can't be assigned to 'XFile'

I have a function to save network image to local cache files, but I have a trouble when store the list file that I downloaded to List<XFile>. Here is my download function:

List<XFile>? imageFileList = [];
Future<File> downloadImage(url, filename) async {
    var httpClient = HttpClient();
    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();
      var bytes = await consolidateHttpClientResponseBytes(response);
      final dir = await getTemporaryDirectory();
      File file = File('${dir.path}/$filename');
      await file.writeAsBytes(bytes);
      print('downloaded file path = ${file.path}');
      return file;
    } catch (error) {
      print('download error');
      return File('');
    }
  }

is there any way so I can save the file to imageFileList as :

imageFileList!.add(file);


Solution 1:[1]

XFILE file = new XFile(file.path);

Conveet file to XFILE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahesh