DEV Community


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

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

Define required variables

var  imageUrl="";
  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();
      String fileName=imageUrl.substring(imageUrl.lastIndexOf("/")+1);, "${dir.path}/$fileName",onReceiveProgress: (rec,total){
        setState(() {
          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)

Editor guide