元素类型'String'不能分配给列表类型'DropdownMenuItem <String>'

时间:2020-05-22 20:50:05

标签: flutter dart

我想用一些字符串填充Flutter中的DropdownButton,但出现错误

select a.*, ( select cat from b where b.name = a.name and b.date_changed <= a.date order by b.date_changed desc limit 1 ) cat from a 用字符串归档列表时

这是我的代码段:

The element type 'String' can't be assigned to the list type 'DropdownMenuItem<String>'

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

items应该是List中的DropdownMenuItem<String>,而不是仅带有“-”的List<String>

DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 16,
      style: TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        setState(() {
          dropdownValue = newValue;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );

请参阅此处: https://api.flutter.dev/flutter/material/DropdownMenuItem/DropdownMenuItem.html

答案 1 :(得分:0)

DropdownButton中的项目应在列表中。注意下面的示例代码中我如何将地图转换成列表。

_currencies是一个字符串数组。

items: _currencies.map((String value) {
                              return DropdownMenuItem<String>(
                                value: value,
                                child: Text(
                                  value,
                                  style: textStyle,
                                ),
                              );
                            }).toList(),