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: