Convert image URL to File

Add below in pubspec.yaml:

http: ^0.12.1
path_provider: ^1.6.7


import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:math';

Future<File> urlToFile(String imageUrl) async {// generate random number.
var rng = new Random();// 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 = new File('$tempPath'+ (rng.nextInt(100)).toString() +'.png');// call http.get method and pass imageUrl into it to get response.
http.Response response = await http.get(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;
}


Sign In or Register to comment.