Download Files in Flutter with Progress using Dio

This Article is posted by abbas.devcode at 5/8/2020 2:43:03 PM

Hi, welcome back! In this tutorial, I'll show you how you can download files in your flutter app using **Dio** package. We are going to store the downloaded file in the ApplicationDirectory using the **path_provider** package. Before showing you the code, let me show what exactly we are going to achieve by the end of this tutorial. font-weight: bolder;](STEPS TO REPRODUCE: font-weight: bolder;]( font-weight: bolder;]( - Add the package dependencies to your pubspec.yaml file - Use the following code in your main.dart file to achieve results as shown in the video. (Don't miss the comments in the code to understand better) _MyHomePageState(); } class _MyHomePageState extends State { bool downloading = false; String progress = '0'; bool isDownloaded = false; String uri = 'https://file-examples.com/wp-content/uploads/2017/10/file-example_PDF_1MB.pdf'; // url of the file to be downloaded String filename = 'test.pdf'; // file name that you desire to keep // downloading logic is handled by this method Future downloadFile(uri, fileName) async { setState(() { downloading = true; }); String savePath = await getFilePath(fileName); Dio dio = Dio(); dio.download( uri, savePath, onReceiveProgress: (rcv, total) { print( 'received: ${rcv.toStringAsFixed(0)} out of total: ${total.toStringAsFixed(0)}'); setState(() { progress = ((rcv / total) * 100).toStringAsFixed(0); }); if (progress == '100') { setState(() { isDownloaded = true; }); } else if (double.parse(progress) getFilePath(uniqueFileName) async { String path = ''; Directory dir = await getApplicationDocumentsDirectory(); path = '${dir.path}/$uniqueFileName.pdf'; return path; } @override Widget build(BuildContext context) { print('build running'); return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Padding( padding: const EdgeInsets.all(8.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('$progress%'), isDownloaded ? Text( 'File Downloaded! You can see your file in the application\'s directory', ) : Text( 'Click the FloatingActionButton to start Downloading!'), ], ), ), ), floatingActionButton: FloatingActionButton( child: Icon(Icons.play_arrow), onPressed: () async { downloadFile(uri, filename); }), ); } }` I hope you liked the example! Keep Fluttering and I welcome any doubts in the comments!

Tags: file download in flutter



3 Comments
Login to comment.
Recent Comments

siffatahmed123 at 10/5/2022

@angemelisk your response is wrong because its not receiving content length as a result its showing bytes downloaded with negative sign because total bytes return -1 when file size is unknown at start of download

Login to Reply.

poorva2097 at 12/17/2020

When I am clicking on floating button the progress text is showing -10800%. Could you please help me to solve this issue.

Login to Reply.

angemelisk at 12/24/2020

because the string uri is not valid data. put this url https://file-examples-com.github.io/uploads/2017/10/file-example_PDF_1MB.pdf in your string uri variable





© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com