I'm trying to parse a HTML document, get all the image-sources and then add the size of all pictures together. Parsing the document works fine, so does getting the image-sources and getting their size.
main(){
print("Please input an URL");
var url = stdin.readLineSync();
getImageUrls(url).then((list) {
list.forEach((imageUrl) {
getImageSize(imageUrl).then((r) {
print("$imageUrl, Size: $r bytes");
});
});
});
}
Future<int> getImageSize(String url){
final completer = new Completer();
new StreamController()..stream.listen((str) => completer.complete(imageSize(str)))..add(url);
return completer.future;
}
imageSize(String url){
return http.get(url).then((response) => response.bodyBytes.length);
}
I'm struggeling to add the size of each individual image together into one integer. I figured I could do something like this
main(){
int total = 0;
print("Please input an URL");
var url = stdin.readLineSync();
getImageUrls(url).then((list) {
list.forEach((imageUrl) {
getImageSize(imageUrl).then((r) {
print("$imageUrl, Size: $r bytes");
total += r;
});
});
});
print(total);
}
but then I'd need wait for getImageUrls to finish before Im able to print my total.
Can anyone push me in the right direction? I feel like I'm missing something really obvious.