Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

type 'String' is not a subtype of type 'int' of 'index' in a nested JSON

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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"];

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading