I’m trying multiple request in a controller.
var requests = [];
for(var url in urls){
requests.add(http.get(Uri.parse(url.split(',')[0])));
}
var results = await Future.wait(requests as Iterable<Future>);
for(int i = 0; i < results.length; i++){
saveData(urls[i].split(',')[1], results[i]);
}
So it give an error.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: type ‘List’ is not a subtype of type ‘Iterable<Future>’ in type cast
2
Answers
The
Future.wait()
method expects anIterable<Future>
as an argument, but you’re passing aList
instead. Just remove casting:Or you can directly run for loop inside the argument:
2 small changes needed:
var requests = [];
withList<Future<dynamic>> requests = [];
requests as Iterable<Future>
withrequests