Google Dart删除DropDown重复项

时间:2014-08-11 15:40:21

标签: json drop-down-menu map hashmap dart

我试图从地图中删除重复项以填充下拉列表。但是,我还没能成功删除它们。下拉列表填充的数据量(JSON)是实际存在的两倍。如果有一个简单的方法,请告诉我。另外,如果您需要更多信息,请告诉我。

飞镖码:

var jsonString = response;
   //var dropDownValue = shroot.querySelector("#asset");
   SelectElement dropDown = shroot.querySelector("#asset");

   Map jsonObject = JSON.decode(jsonString) as Map;
       dropDownList = jsonObject["eee"] as List<Map>;

   LinkedHashMap<String, Map> dataMap = new LinkedHashMap<String, Map>();

   for(Map d in dropDownList)
    {
      dropDown.children.add(new OptionElement(data: d['displayName'], value: d['displayName']));

      print(d);
      print(d["id"]);
      print(d["displayName"]);
    }

Dart Attempt 2:

var ddValues = dropDownList
     // extract the 'displayValue'
     .map((e) => e['displayName'])
     // create a set to eliminate duplicates
     .toSet().toList()
     // sort the result 
     // sort changes the list it is called - it doesn't return a new list
     // therefore we have to chain it using `..` instead of `.`
     ..sort();

     ddValues.forEach((e) {
       print(e);
      dropDown.children.add(new OptionElement(data: e, value: e));
     });

1 个答案:

答案 0 :(得分:1)

目前还不完全清楚你的数据是怎样的,但我想你想要像

这样的东西
void main() {

  // to make the code here shorter I remove the elements from JSON that don't have any effect on the processing. This code should work the same with the full JSON posted in your question.
  var values =  {
                 "serviceResponseValue":[
                    {"displayName":"name"},
                    {"displayName":"name"},
                    {"displayName":"name1"},
                    {"displayName":"Processes"}
                 ],
                 "messages":{"messages":[]}
              };


  var ddValues = values['serviceResponseValue']
  // extract the 'displayValue'
  .map((e) => e['displayName'])
  // create a set to eliminate duplicates
  .toSet().toList()
  // sort the result 
  // sort changes the list it is called - it doesn't return a new list
  // therefore we have to chain it using `..` instead of `.`
  ..sort();

  ddValues.forEach((e) {
    print(e);
    //dropDown.children.add(new OptionElement(data: e, value: e));
  });
}