如何基于其他下拉列表选择将本地Json加载到Flutter DropDown?

时间:2018-12-20 14:35:25

标签: json list dart flutter dropdown

我有一个本地json文件,根据公司名称和/或分支名称,我需要将它们加载到Flutter中的2个不同的下拉列表中。

1)公司下拉列表: 它将包含/显示带有CompanyCode的CompanyName列表;

示例:公司A(01)

2)分支下拉列表:  它将包含/显示带有基于所选公司名称的BranchCode的BranchName列表;

示例:第一分支(0001)

我的问题是我可以加载本地Json文件并添加到Map,但是如何添加到列表中以便可以在Flutter中加载下拉列表?

{  
   "branch":[  
      {  
         "companyCode”:”01”,
         "companyName”:”Comapmy A”,
         "branchCode”:”0001”,
         "branchName”:”First Branch“
      },
      {  
         "companyCode”:”01",
         "companyName”:”Company A”,
         "branchCode”:”0002”,
         "branchName”:”Second Branch”
      },
      {  
         "companyCode”:”02”,
         "companyName”:”Company B”,
         "branchCode”:”0001”,
         "branchName”:”First Branch”
      }
   ]
}


String jsonCompany = await rootBundle.loadString("packages/capi/company.json");
Map _mapCompnay = jsonDecode(jsonCompany);

1 个答案:

答案 0 :(得分:2)

这里有json对象:

<some-tag-that-i-need>
  <MYUSERNAME_CONSTANT>MYUSERNAME</MYUSERNAME_CONSTANT>
  <MYPASSWORDHASH_CONSTANT>MYPASSWORDHASH</MYPASSWORDHASH_CONSTANT>
</some-tag-that-i-need>
<servers>
    <server>
      <username>${MYUSERNAME_CONSTANT}</username>
      <password>${MYPASSWORDHASH_CONSTANT}</password>
      <id>central</id>
    </server>
    <server>
      <username>${MYUSERNAME_CONSTANT}</username>
      <password>${MYPASSWORDHASH_CONSTANT}</password>
      <id>snapshots</id>
    </server>
    ...
    <server>
      <username>${MYUSERNAME_CONSTANT}</username>
      <password>${MYPASSWORDHASH_CONSTANT}</password>
      <id>maven</id>
    </server>
</servers>

接下来,从Map _mapCompany = jsonDecode(jsonCompany); 对象获取数组:

branch

仅此而已,现在您可以使用列表填充您的DropDown:

List list = _mapCompany["branch"];