簡體   English   中英

如何從列表中查找值<map<string, object> > 在飛鏢(撲)? </map<string,>

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM