简体   繁体   English

在 Flutter 中为 DropdownButton 创建项目列表

[英]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.

相关问题 无法使用 Flutter 中的对象列表创建 DropdownButton - Can't create DropdownButton with List of objects in Flutter 有没有办法在 Flutter 中的 DropdownButton 菜单项列表中添加圆角? - Is there a way to add rounded corners to a DropdownButton menu item list in Flutter? 如何使用颤振中的列表中的 JSON 数据列表创建 DropdownButton - How to Create DropdownButton with a list of JSON data within a list in flutter 数据表中的 Flutter DropdownButton,列表中的 DropdownButton 选项 - Flutter DropdownButton in DataTable, DropdownButton options from list 无法选择 Flutter Dropdownbutton 的项目 - Not able to select item of Flutter Dropdownbutton 所选项目未显示在 Flutter 的 DropdownButton 中 - Selected item not showing in DropdownButton in Flutter Flutter - DropdownButton 未显示所选项目 - Flutter - DropdownButton not showing selected item 如何使用 JSON 数据列表创建 DropdownButton,我希望它在 Flutter 中填充我的 DropDownButton - How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter 在DropdownButton中选择项目会导致Flutter引发错误 - Selection of Item in DropdownButton causes Flutter to throw error Flutter - 如何对齐从 DropdownButton 中选择的项目? - Flutter - How to align selected item from DropdownButton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM