3

Please help me... I just tried this code:

**try {
      final response = http.get(url);                       // Giving error here 
      final extractedData = json.decode(response.body);
    } catch (error) {
      throw error;
    }**

Error Message "message": "The getter 'body' isn't defined for the type 'Future'.\nTry importing the library that defines 'body', correcting the name to the name of an existing getter, or defining a getter or field named 'body'.",

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134

1 Answers1

4
try {
      final response = await http.get(url);   
      final extractedData = json.decode(response.body);
    } catch (error) {
      throw error;
    }

You get this error because get() returns a Future<Response>, meaning it is asynchronous so you need to use async/await to be able to get the Response object and then call body.

Please check the following:

https://dart.dev/codelabs/async-await

https://stackoverflow.com/a/748189/7015400

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134