[英]Create Item list for DropdownButton in Flutter
I'm back with another issue with the DropdownButton.我又遇到了 DropdownButton 的另一个问题。
The DropdownButton is not enabled. DropdownButton 未启用。 I found this in api.flutter.dev
我在 api.flutter.dev 中找到了这个
If the onChanged callback is null or the list of items is null then the dropdown button will be disabled, ie its arrow will be displayed in grey and it will not respond to input.
如果 onChanged 回调是 null 或项目列表是 null 那么下拉按钮将被禁用,即它的箭头将显示为灰色并且它不会响应输入。
Here is my code now:这是我现在的代码:
return new DropdownButton<String>(
hint: new Text("Select Agency"),
value: _currentAgency,
onChanged: changedDropDownAgency,
items: snapshot.data.docs.forEach((document) {
return new DropdownMenuItem<String>(
value: document.data()['name'],
child: new Text(document.data()['name']),
);
}),
);
void changedDropDownAgency(String selected_agency) {
setState(() {
_currentAgency = selected_agency;
});
globals.selectedAgency = selected_agency;
}
The forEach loop runs fine and in debug mode I can see that there is data in the document object. forEach 循环运行良好,在调试模式下我可以看到文档 object 中有数据。 I don't know how to debug the DropdownButton code to see why the button is not active.
我不知道如何调试 DropdownButton 代码以查看按钮未激活的原因。 Any suggestions would be helpful.
任何的意见都将会有帮助。 Thanks.
谢谢。
forEach()
on Iterables does not return any value (see: https://api.dart.dev/stable/2.10.5/dart-core/Iterable/forEach.html ), and thus items
is null and the DropdownButton
is disabled. forEach()
on Iterables does not return any value (see: https://api.dart.dev/stable/2.10.5/dart-core/Iterable/forEach.html ), and thus items
is null and the DropdownButton
is disabled . Use map
instead ( https://api.dart.dev/stable/2.10.5/dart-core/Iterable/map.html ). Use
map
instead ( https://api.dart.dev/stable/2.10.5/dart-core/Iterable/map.html ). Example:例子:
snapshot.data.docs.map<DropdownMenuItem<String>>((document) {
return new DropdownMenuItem<String>(
value: document.data()['name'],
child: new Text(document.data()['name']),
);
}).toList(),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.