如何过滤和删除重复的JSON列表?

时间:2018-11-22 14:33:02

标签: list filter dart flutter dropdown

我有一个本地JSON文件,如下所示,并使用“ await rootBundle.loadString(****)”将其加载到flutter项目中。我将地图解码为“ 地图_mapCompany = jsonDecode(jsonComapany)”,然后将地图分配为“ _tmpCompany = _mapComapany [“ companyBranch”];“

现在,我需要使用过滤器来获取[companyName],还需要删除重复的[companyName]并加载到下拉按钮。

我使用var“ _result = new Collection(_tmpCompany).distinct()”删除重复项,但是如何将_result.toList()分配给列表_comapnyName;或下拉按钮?

一旦第一个下拉列表加载,我将在change事件上使用过滤器加载分支列表。我该怎么办?

{  
   “companyBranch”:[  
      {  
         "companyCode":"1",
         "companyName”:”COMAPANY A”,
         "branchCode”:”855”,
         "branchName”:”BRANCH A1”
      },
      {  
         "companyCode":"1",
         "companyName”:”COMPANY A”,
         "branchCode":"856",
         "branchName”:”BRANCH A2“
      },
      {  
         "companyCode":"10",
         "companyName”:”COMAPNY B”,
         "branchCode":"860",
         "branchName”:”BRANCH B1”
      },
      {  
         "companyCode":"10",
         "companyName”:”COMPANY B”,
         "branchCode":"861",
         "branchName”:”BRANCH B2”
      },

1 个答案:

答案 0 :(得分:2)

这应该做你想要的

var uniqueCompanyNames = _tmpCompany.map<String>(
    (c) => (c as List)['companyName'] as String)
    .toSet().toList()