fluttercommunity / fluttercommunity/flutter_downloader

get progress for many tasks

Open
#319 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
940
Forks
561
Avg merge
1h 18m
Merged PRs (30d)
1

Description

I'm working on Download Manager app using this package, it's awesome that it gives us a lot of options such as pause, resume, retry, cancel, remove and more, but the problem I'm facing is a progress problem, I'm using a download model file to define a download

```
import 'package:flutter/material.dart';

class Download {
final String name;
final String size;
final String status;
final String timeLeft;
final String transferRate;
final String type;
final String path;
final String icon;
int progress;

Download({
@required this.name,
@required this.size,
@required this.status,
@required this.timeLeft,
@required this.transferRate,
@required this.type,
@required this.path,
@required this.icon,
@required this.progress,
});
}
```
and downloads provider file to get all downloads and handle all download actions

```
import 'dart:isolate';
import 'dart:ui';

import 'package:idminternetdownloadmanager/models/download.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:http/http.dart' as http;
import 'package:ext_storage/ext_storage.dart';

class DownloadsProvider with ChangeNotifier {
List _downloads = [];

List get downloads {
return [..._downloads];
}

void addDownload(String url) async {
var taskId;
//set name
final name = url.split('/').last;
//set type
final type = url.split('.').last;
//set icon
final icon = type == 'pdf'
? 'lib/assets/pdf.png'
: type == 'txt'
? 'lib/assets/txt.png'
: type == 'docx' || type == 'doc'
? 'lib/assets/doc.png'
: type == 'zip' || type == 'rar'
? 'lib/assets/rar.png'
: type == 'iso'
? 'lib/assets/iso.png'
: type == 'tif' ||
type == 'jpg' ||
type == 'gif' ||
type == 'png' ||
type == 'raw'
? 'lib/assets/png.png'
: type == 'flv' ||
type == 'avi' ||
type == 'mov' ||
type == 'mpeg' ||
type == 'mp4' ||
type == 'ogg' ||
type == 'wmv' ||
type == 'webm' ||
type == '3gp'
? 'lib/assets/mp4.png'
: type == 'mp3' ||
type == 'wma' ||
type == 'mid' ||
type == 'wav'
? 'lib/assets/mp3.png'
: type == 'java'
? 'lib/assets/java.png'
: type == 'rss'
? 'lib/assets/rss.png'
: type == 'php'
? 'lib/assets/php.png'
: type == 'xml'
? 'lib/assets/xml.png'
: type == 'html'
? 'lib/assets/html.png'
: type == 'css'
? 'lib/assets/css.png'
: 'lib/assets/unknown.png';
//set size
String size;
http.Response r = await http.head(url);
final fileSize = int.parse(r.headers["content-length"]);
if (fileSize >= 1024 && fileSize < 1048576) {
size = '${(fileSize / 1024).toStringAsFixed(2)} KB';
} else if (fileSize >= 1048576 && fileSize < 1073741824) {
size = '${(fileSize / 1048576).toStringAsFixed(2)} MB';
} else {
size = '${(fileSize / 1073741824).toStringAsFixed(2)} G';
}
//set downloads directory path
String path = await ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);
//start download
final storagePermission = await Permission.storage.request();
if (storagePermission.isGranted) {
taskId = await FlutterDownloader.enqueue(
url: url,
savedDir: path,
showNotification:
true, // show download progress in status bar (for Android)
openFileFromNotification:
true, // click on notification to open downloaded file (for Android)
);
} else {
return;
}


// trying a different way to get progress
try {
final tasks = await FlutterDownloader.loadTasksWithRawQuery(
query: 'SELECT * FROM task WHERE status=3 AND progress<>0');
final task = tasks.firstWhere((task) => task.taskId == taskId);
int progress = task.progress;
print(progress);
} catch (e) {
print(e);
} // and it doesn't work


_downloads.add(Download(
name: name,
size: size,
status: 'Unknown',
timeLeft: 'Unknown',
transferRate: 'Unknown',
type: type,
path: path,
icon: icon,
progress: 5555,
));
notifyListeners();
}
}
```

and this is a photo of what I'm building
![image](https://user-images.githubusercontent.com/31194429/88858452-4a7caa00-d1f8-11ea-865f-63cf932116bf.png)

and this my ui
```
@override
Widget build(BuildContext context) {
final downloadList = Provider.of(context).downloads;
return GestureDetector(
onTapDown: storePosition,
onLongPress: () {
setState(() {
rowColor = Colors.blue;
});
showPopupMenu();
},
//showPopupMenu,
child: Container(
//height: 25,
decoration: BoxDecoration(
color: rowColor,
border: Border(
bottom: BorderSide(width: 1, color: Colors.grey[350]),
),
),
child: Row(
children: [
NameRowContainer(
data: downloadList[widget.index].name,
icon: downloadList[widget.index].icon,
),
RowContainer(
data: downloadList[widget.index].size,
lastOne: false,
width: 100),
RowContainer(
// I want to show progress here
data: downloadList[widget.index].status,
lastOne: false,
width: 100),
RowContainer(
data: downloadList[widget.index].timeLeft,
lastOne: false,
width: 85),
RowContainer(
data: downloadList[widget.index].transferRate,
lastOne: true,
width: 120),
],
),
),
);
}
```

so the issue is I can't get progress for each task and store it is a progress variable in download model soo then I can show it to the user in the data table as shown in the photo above
the way you provided to get progress it works for single file not all files, or at least I can't work around it

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.