I have this json from a weather api using Caracas Venezuela as location and I want to get daily_chance_of_rain and daily_will_it_rain:
"forecast": {
"forecastday": [
{
"day": {
"daily_will_it_rain": 1,
"daily_chance_of_rain": 97,
//The Json is longer, so I cropped it
I try to get it like this because those values are int:
int? will_it_rain;
int? chance_of_rain;
var searchResult = await http
.get(Uri.parse(searchApiUrl "Caracas" "&days=1&aqi=no&alerts=no"));
var result = json.decode(searchResult.body);
will_it_rain =
result["forecast"]["forecastday"]["day"]["daily_will_it_rain"];
chance_of_rain =
result["forecast"]["forecastday"]["day"]["daily_chance_of_rain"];
But I get:
Exception has occurred.
_TypeError (type 'String' is not a subtype of type 'int' of 'index')
Why do I get that error is those values are int not strings?
CodePudding user response:
you need to define index when returning result like this:
result["forecast"]["forecastday"][0]["day"]["daily_will_it_rain"];
or you can use iterate like this example.
CodePudding user response:
This error means that you did this someList['some string']
, that is to say, you passed a string into an index, in this case, forecastday
is actually a list of days, so you need to do something.
will_it_rain =
result["forecast"]["forecastday"][0]["day"]["daily_will_it_rain"];
chance_of_rain =
result["forecast"]["forecastday"][0]["day"]["daily_chance_of_rain"];