json.decode(response.body)..."Here response.body is not working..."

1,174
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

Share:
1,174
shivam goyal
Author by

shivam goyal

Updated on December 02, 2022

Comments

  • shivam goyal
    shivam goyal over 1 year

    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'.",