skip to Main Content

I am new to flutter development and is experimenting with how to use the flutter HTTP package 0.12.0+2.

If the response looks like this…

{
    "coord": {
        "lon": -76.8403,
        "lat": 38.9649
    },
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 93.47,
        "feels_like": 99.34,
        "temp_min": 88.84,
        "temp_max": 97.74,
        "pressure": 1017,
        "humidity": 46
    },
    "visibility": 10000,
    "wind": {
        "speed": 1.99,
        "deg": 304,
        "gust": 1.99
    },
    "clouds": {
        "all": 1
    },
    "dt": 1626462013,
    "sys": {
        "type": 2,
        "id": 2030617,
        "country": "US",
        "sunrise": 1626429293,
        "sunset": 1626481895
    },
    "timezone": -14400,
    "id": 4369076,
    "name": "Seabrook",
    "cod": 200
}

Here is the code I have. Instead of printing all the data, how do I print only temp inside main

void getData() async {

    Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude={part}&appid=b29e187fed23cf37dc160e6c115a270d');
    // print(response.body);
    Map data = jsonDecode(response.body);
    print(data);
  }

3

Answers


  1. You can use:

    void getData() async {
      Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude={part}&appid=b29e187fed23cf37dc160e6c115a270d');
      // print(response.body);
      Map data = jsonDecode(response.body);
      print(data['main']); //Will return [temp], [feels_like], [temp_min], etc..
      print(data['main']['temp']); //Will return [93.47]
    }
    
    Login or Signup to reply.
  2. Create Model Class here you can convert json to dart
    https://javiercbk.github.io/json_to_dart/

    Future<YourmodelName>getData() async {
    YourmodelName data
    Response response = await get('https://api.openweathermap.org/data/2.5/onecall? 
    lat=38.964882&lon=-76.840271&exclude= 
     {part}&appid=b29e187fed23cf37dc160e6c115a270d');
    if (response.statusCode == 200) {
    data = YourmodelName.fromJson(response.data);;
    print(data.main.temp);}
    return data;
    }
    
    Login or Signup to reply.
  3. You can access the values of each fields by using the operator [$field] on the decoded json. Something like this:

    void getData() async {
    
     Response response = await get('https://api.openweathermap.org/data/2.5/onecall?lat=38.964882&lon=-76.840271&exclude=.{part}&appid=b29e187fed23cf37dc160e6c115a270d');
     // print(response.body);
     Map data = jsonDecode(response.body);
     print(data['main']); // prints out { temp: 93.47, ...., humidity: 46 }
     // what you want..
     final mainTemp = data['main']['temp'];
     print(mainTemp); // prints out 93.47.
     print(data);                                                            
    }
    

    So, that’s how you access the fields of of the decoded json-string response.

    If you plan to use these received values throughout your app, consider changing the received response into an Interface which will provide you with more flexibility and also makes your code look cleaner.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search