Jaimin Modi
Jaimin Modi

Reputation: 1667

Flutter : Downloading multiple files

I have used Dio package to download a file as below :

Future<void> downloadFile() async {
// requests permission for downloading the file
bool hasPermission = await _requestWritePermission();
if (!hasPermission) return;

Dio dio = Dio();
try {
  var dir = await getApplicationDocumentsDirectory();
  await dio.download(uri, "${dir.path}/eng_json.json",
      onReceiveProgress: (rec, total) {
    print("Path >>> " + "${dir.path}/eng_json.json");
    setState(() {
      downloading = true;
    });
  });
} catch (e) {
  print("Error >> " + e.toString());
}
setState(() {
  downloading = false;
  print("Download Completes");
    });
  }

But I have to download multiple files. Is there any support in Dio package to download multiple files? Or Any other better way to handle downloading multiple files in Flutter application?

Upvotes: 1

Views: 2400

Answers (1)

Thiago Fontes
Thiago Fontes

Reputation: 375

iTo nest multiple calls you can use:

Future.wait([]);

Like this:

Future.wait([dio.download(firstUri, path1), dio.download(secondUri, path2)];

or if you want to start one download after the other:

dio.download(uri1, path1).then((value) => dio.download(uri2, path2));

Those just use darts async functionalities so I believe you can use this approach with any async call.

Upvotes: 4

Related Questions