Flutter:下载多个文件



我使用Dio包下载了如下文件:

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");
});
}

但我必须下载多个文件。Dio软件包是否支持下载多个文件?或者有其他更好的方法来处理Flutter应用程序中的多个文件下载吗?

i嵌套多个可以使用的调用:

Future.wait([]);

像这样:

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

或者如果你想一个接一个地开始下载:

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

这些只是使用了飞镖异步功能,所以我相信你可以在任何异步调用中使用这种方法。

最新更新