urlToFile static method
File file = await Mixins.urlToFile('FILE-URL');
Implementation
static Future<File> urlToFile(String imageUrl,
{String format = 'png'}) async {
try {
// get temporary directory of device.
Directory tempDir = await getTemporaryDirectory();
// get temporary path from temporary directory.
String tempPath = tempDir.path;
// create a new file in temporary path with random file name.
File file =
File('$tempPath${DateTime.now().millisecondsSinceEpoch}.$format');
// call http.get method and pass imageUrl into it to get response.
http.Response response = await http.get(Uri.parse(imageUrl));
// write bodyBytes received in response to file.
await file.writeAsBytes(response.bodyBytes);
// now return the file which is created with random name in
// temporary directory and image bytes from response is written to // that file.
return file;
} catch (e) {
rethrow;
}
}