The original

Medium.com/halkbank-mo…

code

Github.com/deremakif/F…

reference

  • Pub. Dev/packages/pa…
  • Pub. Dev/packages/fl…
  • Pub. Dev/packages/fl…
  • Pub. Dev/packages/op…
  • Pub. Dev/packages/PE…

The body of the

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 like 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 the path_Provider package. And calls the initPlatformState method in 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; }Copy the code

Now, we have the download folder to save the files. The package download method takes two parameters: THE URL and options. You can customize the options as required.

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,
           );
},
Copy the code

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

If you don’t need the file later, you can delete it after closing the document. It is important not to increase the size of your application.

OpenFile.open('$path/loremipsum.pdf').then((value) {
    File f = File('$path/loremipsum.pdf');
    f.delete();
});
Copy the code
  • Application demo

Source code for the sample project.

  • Lot – deremakif/FlowderSample github.com/deremakif/F…

The elder brother of the © cat

  • ducafecat.tech/

  • github.com/ducafecat

  • Ducafecat WeChat group