DEV Community

loading...

Download Files in Flutter Download PDF , Images

rrtutors profile image rrtutors ・1 min read

In this post we are going to learn how we can download a file in flutter and show its downloading progress.

In the most of the scenarios we get to download file from internet and store that file inside application folder/ External folder.

To download files from internet we are using the dio library. this dio library gives option to get the download progress, which means we can also show the progress of the downloading file.

Suppose we have file to download and written code for get the file, but where will store that file, and how we will store that file. To store the file inside application folder/ External folder we will use Path_provider plugin

Add dependencies to pubspec.yaml file


dev_dependencies:
  flutter_test:
    sdk: flutter
  dio: ^3.0.8
  path_provider: ^1.6.0
Enter fullscreen mode Exit fullscreen mode



Define required variables

var  imageUrl="https://www.itl.cat/pngfile/big/10-100326_desktop-wallpaper-hd-full-screen-free-download-full.jpg";
  bool downloading=true;
  String downloadingStr="No data";
  double download=0.0;
  File f;
Enter fullscreen mode Exit fullscreen mode

Functionality to download file

 Dio dio=Dio();
      var dir=await getApplicationDocumentsDirectory();
      f=File("${dir.path}/myimagepath.jpg");
      String fileName=imageUrl.substring(imageUrl.lastIndexOf("/")+1);
      dio.download(imageUrl, "${dir.path}/$fileName",onReceiveProgress: (rec,total){
        setState(() {
          downloading=true;
          download=(rec/total)*100;
         print(fileName);
          downloadingStr="Downloading Image : "+(download).toStringAsFixed(0);
        });
Enter fullscreen mode Exit fullscreen mode

Example codes
Download files in flutter
Download PDF files flutter


Alt Text

Discussion (0)

pic
Editor guide