[英]How to get selected item's index number in DropdownButton in Flutter?
I populate the dropdown menu with the GET Request's distList.sectionName values.我使用 GET 请求的 distList.sectionName 值填充下拉菜单。 What I am trying to do is catch the index of the selected museum and then send this index's values to the another API call.
我想要做的是捕获所选博物馆的索引,然后将该索引的值发送到另一个 API 调用。 For example;
例如; lets say user is select the "İnte.net Müzesi" in the dropdown menu.
假设用户是 select 下拉菜单中的“İnte.net Müzesi”。 After that I need to locate the position of the "İnte.net Müzesi" in the distList.
之后,我需要在 distList 中找到“İnte.net Müzesi”的 position。 And then catch this two fields: "distId": "MRK" and "sectionId": "INT01" values so I can send these values to another API call.
然后捕获这两个字段:“distId”:“MRK”和“sectionId”:“INT01”值,以便我可以将这些值发送到另一个 API 调用。 How can I achieve this?
我怎样才能做到这一点?
My DropdownButton widget:我的 DropdownButton 小部件:
Container(
child: DropdownButton<String>(
hint: Text("Lütfen listeden seçim yapın."),
items: snapshot.data.distList
.map<DropdownMenuItem<String>>((item) {
return DropdownMenuItem<String>(
value: item.sectionName,
child: Text(item.sectionName));
}).toList(),
value: _currentSelectedValue,
isExpanded: false,
onChanged: (String? value) {
print("Drop Down Selected Museum is $value");
setState(() {
_currentSelectedValue = value;
});
},
),
),
Here is my GET Request response:这是我的 GET 请求响应:
"distList": [
{
"distId": "MRK",
"sectionId": "INT01",
"sectionName": "İnternet Müzesi",
"sectionNameEng": null
},
{
"distId": "IAR",
"sectionId": "IAR01",
"sectionName": "İstanbul Arkeoloji Müzesi",
"sectionNameEng": "İSTANBUL ARCHAEOLOGICAL MUSEUMS"
},
{
"distId": "TPK",
"sectionId": "TPK01",
"sectionName": "İstanbul Topkapı Sarayı Müzesi",
"sectionNameEng": "TOPKAPI PALACE MUSEUM"
},
{
"distId": "MRK",
"sectionId": "CUM01",
"sectionName": "Ankara Cumhuriyet Müzesi",
"sectionNameEng": "MUSEUM OF REPUBLIC OF ANKARA"
}
],
"acknowledge": true,
"message": null,
"requestId": null
}
ok, I found the solution.好的,我找到了解决方案。
in the setState I catch the selectedValue's field like this:在 setState 中,我像这样捕获 selectedValue 的字段:
_currentSelectedValue = value;
var var2 = snapshot.data.distList!.firstWhere(
(e) =>
e.sectionName ==
"$_currentSelectedValue");
print(var2.distId);
print(var2.sectionId);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.