percent property
Returns the overall loading percentage (on a scale from 0.0 to 1.0).
This percentage is calulated as the avarage progress of all tracked _items. This approach ensures:
- It handles cases where the total size of _items are not (yet) known.
- It prevents percantage rollbacks that can occur when we just calulcate it based on loaded / totalBytes when the size only becomes known during the loading process (which is the default case, because when you, for example, initialize a Http Request you dont know the size yet.
Implementation
double get percent {
if (_items.isEmpty) {
return 1.0;
}
if (total == 0) {
return (allItemsFinished) ? 1.0 : 0.0;
}
List<double> itemProgress = [];
for (var item in _items) {
if (item.completedOrFailed) {
itemProgress.add(1.0);
} else if (item.total == 0) {
itemProgress.add(0.0);
} else {
itemProgress.add((item.loaded / item.total.toDouble()).clamp(0.0, 1.0));
}
}
return itemProgress.average().clamp(0.0, 1.0);
}