Reputation: 367
I am trying to download multiple files using dio package. From this git issue I found out that future.wait can be used to achieve this task, though I am able to update multiple files concurrently but cannot update the database. Here's the downloading code snippet
download(Attachment attachment) => work(context, () async {
final dio = Dio();
final path = await getLocalFilePath(attachment);
var downloadRequest = dio.download(
attachment.url,
path,
deleteOnError: true,
onReceiveProgress: (count, total) {
setState(
() {
_downloadProgress[attachment.url] = count / total;
print(_downloadProgress[attachment.url]);
},
);
},
).then(
(value) async {
await ReviewDownloadsDB().add({
'id': attachment.id,
'name': attachment.name,
'url': attachment.url,
'path': path
});
},
);
Future.wait([downloadRequest]);
});
Can someone tell me where I'm going wrong and how can I add the downloaded item to my ObjectDB database after it's downloaded. The complete Code for the screen is present here.
Upvotes: 2
Views: 2386
Reputation: 10443
If you don't mind using a different package, you can use flutter_downloader package to download multiple files concurrently. The default max number of concurrent download tasks set is three(3), you can change this config by following this guide.
You can then use FlutterDownloader.enqueue()
to create download tasks.
final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
headers: {}, // optional: header send with url (auth token etc)
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
Upvotes: 1