Downloading a file in Flutter

MAkif DERE
Halkbank Mobile Tech
2 min readNov 1, 2021

Today I’ll write about the flowder package. I used it to download files from the server. There are lots of ways to do it, and there are more popular packages like flutter_downloader. But I preferred the flowder package because of its simple implementation.

At first, we should create the Downloads folder if it does not exist. To do it, we need to import path_provider package. And call initPlatformState method in initState() of the current page.

Future<void> initPlatformState() async {
_setPath();
if (!mounted) return;
}
void _setPath() async {
Directory _path = await getApplicationDocumentsDirectory();
String _localPath = _path.path + Platform.pathSeparator + 'Download';
final savedDir = Directory(_localPath);
bool hasExisted = await savedDir.exists();
if (!hasExisted) {
savedDir.create();
}
path = _localPath;
}

Now, we have the Downloads folder to save the file. The download method of the package requires two parameters; URL and options. You can customize the options as your needs.

ElevatedButton(
onPressed: () async {
options = DownloaderUtils(
progressCallback: (current, total) {
final progress = (current / total) * 100;
print('Downloading: $progress');
},
file: File('$path/loremipsum.pdf'),
progress: ProgressImplementation(),
onDone: () {
OpenFile.open('$path/loremipsum.pdf');
},
deleteOnCancel: true,
);
core = await Flowder.download(
"https://assets.website-files.com/603d0d2db8ec32ba7d44fffe/603d0e327eb2748c8ab1053f_loremipsum.pdf",
options,
);
},

I used the OpenFile package to open the file when it completes the download process. I also used the percent_indicator package to show the progress.

If you don’t need to use that file later, you can delete the file after closing the document. It is important to not increase the app size.

OpenFile.open('$path/loremipsum.pdf').then((value) {
File f = File('$path/loremipsum.pdf');
f.delete();
});
Demo Of The App
The Screenshot Of The App

You can find the source code of the sample project on this repo.

--

--

Responses (3)