![](/img/trans.png)
[英]How to model class in order to serialize Dart Object (Map<String, List>) into JSON in Flutter?
[英]How to find a value from List<Map<String, Object>> in dart(flutter)?
我有一个这样的所有国家的列表:
List<Map<String, Object>> allCountries = [
{
"name": "Afghanistan",
"code": "AF",
"capital": "Kabul",
"region": "AS",
"currency": {"code": "AFN", "name": "Afghan afghani", "symbol": "؋"},
"language": {"code": "ps", "name": "Pashto"},
"flag": "https://restcountries.eu/data/afg.svg",
"dialling_code": "+93"
},
.................
{
"name": "Zimbabwe",
"code": "ZW",
"capital": "Harare",
"region": "AF",
"currency": {"code": "BWP", "name": "Botswana pula", "symbol": "P"},
"language": {
"code": "en",
"iso639_2": "eng",
"name": "English",
"nativeName": "English"
},
"flag": "https://restcountries.eu/data/zwe.svg",
"dialling_code": "+263"
}
];
我想从原始列表创建另一个List<Map<String, Object>> resultOfQuery
但仅当查询匹配或以国家名称开头时。 如何才能做到这一点?
使用此方法:
List<Map<String, Object>>filterByCountry(List<Map<String, Object>> list, String value){
return list.where((mapObject) => (mapObject["name"] as String).startsWith(value)).toList();
}
你可以这样使用它:
print(filterByCountry(allCountries, "Afghan")); // will return List containing only the Map with the Afghanistan name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.