Convert Image URL to File format in Flutter

Gulshan Yadav
2 min readMay 1, 2020

So, in this article, I will be showing you how you can convert an image URL into a File format in Flutter.

For this purpose, we need to add these two dependencies in pubspec.yaml file.

http: ^0.12.1path_provider: ^1.6.7

http will be used for calling API requests to image URL for getting image bytes.

path_provider will be used for finding the temporary locations on the filesystem of the device to temporarily store an image on the device.

Let’s jump into the coding part.

firstly, you need to import http and path_provider package into your dart file in which you want to perform the task of converting image URL to the File format.

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

here comes the main function which will convert any Image URL to the File format.

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…

--

--

Gulshan Yadav
Gulshan Yadav

Written by Gulshan Yadav

Freelancer - Flutter | Android | Blockchain | NodeJs | Technical Writer | Open Source Contributor | LinkedIn — https://www.linkedin.com/in/mrgulshanyadav

Responses (3)