-3

I wanted to create an app that would tell me weather information, taken from a webscraping.

I made this code and I have no idea why it's giving me "Instance of 'Future'" instead of my value:

import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart' as http;

class Scraper {
 List temp = [];

 Future getData(city) async {
   final response = await http
      .get(Uri.parse('https://www.google.com/search?q=temperature+$city'));

   dom.Document document = parser.parse(response.body);

   var idTemp = document.getElementById('wob_tm');

   return idTemp;
   }
 }

 void main() {
  var tempe = Scraper().getData('São Paulo');
  print(tempe);
}

Why didn't my code work?

Nimantha
  • 5,793
  • 5
  • 23
  • 56
Artur
  • 12
  • 4
  • I understood how Future, Await and Async work, but not how I can solve my problems – Artur Nov 23 '21 at 22:58
  • You are getting the above error because getData() returns future, while you are calling the getData() in the main function. You should use FutureBuilder in order to fetch future data . – dartKnightRises Nov 23 '21 at 23:25
  • https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it, – dartKnightRises Nov 23 '21 at 23:32

1 Answers1

0
void main() async {
  var tempe = await Scraper().getData('São Paulo');
  print(tempe);
}

You need to await your Future and you can only use the await keyword in an async method. If you want to know why, I would suggest you re-read What is a Future and how do I use it?

nvoigt
  • 68,786
  • 25
  • 88
  • 134