1

I have created a List which has URLs inside it. I want to use http.get() to read these URLs. I've tried to use http.Client() but i did not know what to parse in.

My function:

   List url = listear ; // <- URLS 
http.Response response = await http.get(url);
var document = parse(response.body);
var items = document.querySelector("body");
setState(() {
  valueee = items.innerHtml;
});

3 Answers3

0

Use your get method multiple times, each per one request like this:

...
var response = [];
for(final e in url){
  response.push(await http.get(e));
}
...
Anton Marinenko
  • 2,749
  • 1
  • 10
  • 16
0

You can use Future.wait() with collection for, like this :

List<http.Response> results = await Future.wait([for(var url in urls) http.get(url)]);
for (var result in results) {
  var document = parse(result.body);
  var item = document.querySelector("body");
  setState(() {
    valueee += item.innerHtml;
  });
}
Augustin R
  • 7,089
  • 3
  • 26
  • 54
  • how can i then use parse (from html/parser.dart) to do document.outerHtml or results.querySelector –  Feb 21 '20 at 17:33
  • I do not know these packages but I'm sure you can use them as previously inside a for-loop. I tried to write something that could help you but I don't really know what your are trying to achieve. – Augustin R Feb 21 '20 at 17:41
  • got an exception from item.innerhtml was called on null. Seems like item or document dont return any value. –  Feb 22 '20 at 06:32
0

Thank you for your answers i made a new function from your answers.

  Future _initiate() async {
    List urls = [
      'https://stackoverflow.com/questions/60338520/use-a-list-in-http-get/60340538?noredirect=1#comment106745313_60340538',
      'https://stackoverflow.com/questions/60338520/use-a-list-in-http-get/60340538?noredirect=1#comment106745313_60340538'
    ];
    listii = [];
for (var url in urls) {
  listii.add(await http.get(url));

  for (var result in listii) {
    var document = parse(result.body);
    setState(() {
      valueee = document.outerHtml;
      print(' ');
      print(valueee);
      print(' ');
    });
  }
}


 }