0

我又遇到了 DropdownButton 的另一个问题。

DropdownButton 未启用。我在 api.flutter.dev 中找到了这个

如果 onChanged 回调为 null 或项目列表为 null,则下拉按钮将被禁用,即其箭头将显示为灰色并且不会响应输入。

这是我现在的代码:

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;
  }

forEach 循环运行良好,在调试模式下我可以看到文档对象中有数据。我不知道如何调试 DropdownButton 代码以查看按钮未激活的原因。任何的意见都将会有帮助。谢谢。

4

1 回答 1

1

forEach()on Iterables 不返回任何值(请参阅:https ://api.dart.dev/stable/2.10.5/dart-core/Iterable/forEach.html ),因此items为 null 并且DropdownButton被禁用。改用maphttps://api.dart.dev/stable/2.10.5/dart-core/Iterable/map.html)。例子:

snapshot.data.docs.map<DropdownMenuItem<String>>((document) {
    return new DropdownMenuItem<String>(
        value: document.data()['name'],
        child: new Text(document.data()['name']),
    );
}).toList(),
于 2021-05-12T22:07:50.810 回答