I have this JSON response:

{
    "suggestions": [
        {
            "label": "United States, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_rNsDLtnazM.kjVC-K6YWOA",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>"
            },
            "matchLevel": "state"
        },
        {
            "label": "United States, <b>New York</b>, <b>New York</b>, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_YpQcXqbaOb.I4m5EW8BHEC",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "<b>New York</b>",
                "city": "<b>New York</b>",
                "postalCode": "10007"
            },
            "matchLevel": "city"
        },
        {
            "label": "United States, <b>New York</b>, <b>New York</b>",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_ABhedB4xGEL83YY5az47iD",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "<b>New York</b>"
            },
            "matchLevel": "county"
        },
        {
            "label": "United Kingdom, NE27 0, North Shields, <b>New York</b> Way",
            "language": "en",
            "countryCode": "GBR",
            "locationId": "NT_B24AwYSzW4TK04x9UiKMoD",
            "address": {
                "country": "United Kingdom",
                "state": "England",
                "county": "Tyne and Wear",
                "city": "North Shields",
                "district": "North Shields",
                "street": "<b>New York</b> Way",
                "postalCode": "NE27 0"
            },
            "matchLevel": "street"
        },
        {
            "label": "United States, <b>New York</b>, Buffalo, <b>New York</b> State Thruway",
            "language": "en",
            "countryCode": "USA",
            "locationId": "NT_-WyF-KGZ-pqF8zQV6NegHD",
            "address": {
                "country": "United States",
                "state": "<b>New York</b>",
                "county": "Erie",
                "city": "Buffalo",
                "district": "Grant Ferry",
                "street": "<b>New York</b> State Thruway",
                "postalCode": "14213"
            },
            "matchLevel": "street"
        }
    ]
}

And in code I would like to display only values which matchLevel type is city.

So far I've been trying this but it does not work. What am I doing wrong here?

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      rest.forEach((val) => filteredList.add(rest.where((val) => val["matchLevel"] == "city")));
      print(filteredList);

      this.list =
          filteredList.map<Suggestions>((json) => Suggestions.fromJson(json)).toList();

Tried also this:


Solution 1: Peter Haddad

Change this:

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      rest.forEach((val) => filteredList.add(rest.where((val) => val["matchLevel"] == "city")));
      print(filteredList);

into this:

var data = json.decode(res.body);
      var rest = data["suggestions"] as List;
      var filteredList;
      filteredList = rest.where((val) => val["matchLevel"] == "city");
      print(filteredList);

From the docs:

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable may invoke the supplied function test multiple times on the same element.

Therefore when you use forEach() and where() you will get the same result multiple times.