original
https://medium.com/halkbank-mobile-tech/downloading-a-file-in-flutter-d6762825c0a4
Code
https://github.com/deremakif/FlowderSample
refer to
- https://pub.dev/packages/path_provider
- https://pub.dev/packages/flutter_downloader
- https://pub.dev/packages/flowder
- https://pub.dev/packages/open_file
- https://pub.dev/packages/percent_indicator
text
Today I am going to write an article about flowder package . I use it to download files from the server. There are many ways to do this, and there are more popular packages such as flutter_downloader . But I prefer the flowder package because of its simple implementation.
First, if the download folder does not exist, we should create it. To do this, we need to import path_provider package . And call the initPlatformState
method initState()
on 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 download folder to save the file. The download method of the package requires two parameters: URL and options. You can customize the options according to 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 use OpenFile package package to open the file when it finishes downloading process. I also used the package to show the progress.
If you do not need to use the file in the future, you can delete the file after closing the document. It is important not to increase the size of the application.
OpenFile.open('$path/loremipsum.pdf').then((value) {
File f = File('$path/loremipsum.pdf');
f.delete();
});
- Application demo
The source code of the sample project.
- GitHub - deremakif/FlowderSample
https://github.com/deremakif/FlowderSample
© Cat brother
- https://ducafecat.tech/
- https://github.com/ducafecat
- WeChat group ducafecat
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。