【问题标题】:Flutter - There should be exactly one item with [DropdownButton]'s valueFlutter - 应该只有一项具有 [DropdownButton] 的值
【发布时间】:2021-08-29 02:35:27
【问题描述】:

请有人帮助我。我在 StatefullWidget 上使用地图键和值创建了 DropdownButton

我在这里使用地图是因为我将选定的键值发送到父小部件。 我收到错误: 应该只有一项具有 [DropdownButton] 的值:Все подряд。 检测到 0 个或 2 个或多个 [DropdownMenuItem] 具有相同的值

删除 DropdownButton 小部件中的值后,不会引发任何错误

title: Container(
        child: DropdownButtonHideUnderline(
          child: DropdownButton(
            **value: _options[dropDownValue]**,

But in this case there is no value for the dropdown button

完整代码:

class _MyAppBarState extends State<MyAppBar> {
  String dropDownValue = 'created';

  Map<String, String> _options = {
    'rating': 'Лучшие',
    'seen': 'Интересные',
    'created': 'Все подряд',
  };

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: Color(0xff62858F),
      elevation: 20,
      shadowColor: Colors.grey,
      title: Container(
        child: DropdownButtonHideUnderline(
          child: DropdownButton(
            value: _options[dropDownValue],
            icon: const Icon(
              Icons.arrow_drop_down,
              color: Colors.white,
              size: 25,
            ),
            iconSize: 24,
            elevation: 2,
            selectedItemBuilder: (BuildContext context) {
              return _options
                  .map((String key, String value) {
                    print(key);
                    print(value);
                    return MapEntry(
                      key,
                      Padding(
                        padding: EdgeInsets.symmetric(vertical: 15),
                        child: Text(
                          value,
                          style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            fontSize: 17,
                          ),
                        ),
                      ),
                    );
                  })
                  .values
                  .toList();
            },
            //underline:,
            items: _options
                .map(
                  (String key, String value) {
                    return MapEntry(
                      key,
                      DropdownMenuItem<String>(
                        value: key,
                        child: Text(
                          value,
                          style: TextStyle(
                            color: Colors.black,
                          ),
                        ),
                      ),
                    );
                  },
                )
                .values
                .toList(),
            onChanged: (String newValue) {
              widget.sortBy(newValue);
              setState(() {
                dropDownValue = newValue;
              });
            },
          ),
        ),
      ),
      actions: <Widget>[
        IconButton(
          icon: Icon(
            Icons.search,
            size: 25,
          ),
          onPressed: () {
            Navigator.of(context).pushNamed(SearchPage.tag);
          },
        )
      ],
    );
  }
}

【问题讨论】:

    标签: flutter dart dropdownbutton


    【解决方案1】:

    将您的 DropdownButton 值属性更改为仅 dropDownValue(而不是 _options[dropDownValue])。

    因为在DropdownMenuItem 中,您将value 设置为keyMapEntry

    【讨论】:

    • 感谢您的解释。现在它正在工作
    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2023-01-17
    • 2020-07-18
    • 2021-12-03
    • 2023-01-05
    • 2020-08-26
    相关资源
    最近更新 更多