【问题标题】:How to add String to Dropdownbutton如何将字符串添加到下拉按钮
【发布时间】:2021-05-12 05:23:29
【问题描述】:

我有带有单位和名称的 map

Map timeUnits = <dynamic, String>{
  TimeUnit.second: 'second',
  TimeUnit.minute: 'minute',
  TimeUnit.hour: 'hour',
  TimeUnit.day: 'day',
  TimeUnit.week: 'week',
  TimeUnit.month: 'month',
  TimeUnit.calendarYear: 'year',
};

如何将字符串添加到下拉按钮? Dropdownbutton 显示此问题“无法将参数类型 'Map' 分配给参数类型 'List'。” 下拉按钮代码:

child: DropdownButton(
              isExpanded: true,
              underline: Container(),
              value: currentUnits[widget.convertorType],
              icon: Padding(
                padding: const EdgeInsets.only(right: 8.0, bottom: 6),
                child: Image.asset("assets/icons/down-arrow2.png"),
              ),
              iconSize: 35,
              elevation: 20,
              style: TextStyle(
                fontSize: 18,
                color: Color.fromRGBO(114,137,218, 1)
              ),
              items: widget.items
                  .map(
                    (e) => DropdownMenuItem(
                      child: e == TimeUnit.calendarYear
                          ? Text("year")
                          : e.toString().length == 3
                              ? Text(e)
                              : Text(e.toString().split(".")[1]),
                      value: e,
                    ),
                  )
                  .toList(),
              onChanged: (newValue) {
                setState(() {
                  updateCurrentUnit(widget.convertorType, newValue);
                  widget.calculate();
                });
              },
            ),

【问题讨论】:

  • 请添加 widget.item 定义的内容

标签: flutter dart


【解决方案1】:

据我了解,这是由于 (items: widget.item) 错误 所以将其更改为 (items: widget.items.entries) 并将 e 更改为 e.key 或 e.value

 DropdownButton(
          isExpanded: true,
          underline: Container(),
          value: currentUnits[widget.convertorType],
          icon: Padding(
            padding: const EdgeInsets.only(right: 8.0, bottom: 6),
            child: Image.asset("assets/icons/down-arrow2.png"),
          ),
          iconSize: 35,
          elevation: 20,
          style: TextStyle(
            fontSize: 18,
            color: Color.fromRGBO(114,137,218, 1)
          ),
          items: widget.items.entries
              .map(
                (e) => DropdownMenuItem(
                  child: e.key == TimeUnit.calendarYear
                      ? Text("year")
                      : e.toString().length == 3
                          ? Text(e.key)
                          : Text(e.value),
                  value: e,
                ),
              )
              .toList(),
          onChanged: (newValue) {
            setState(() {
              updateCurrentUnit(widget.convertorType, newValue);
              widget.calculate();
            });
          },
        )

【讨论】:

  • 显示了这个问题:getter 'entries' 没有为类型'List' 定义。尝试导入定义“条目”的库,将名称更正为现有 getter 的名称,或定义名为“条目”的 getter 或字段。
  • i 在没有条目的情况下开始构建显示“类型 '_CompactIterable' 不是类型 'List' 的子类型”
【解决方案2】:

首先,您必须将 Map 转换为列表,然后创建 dropDownMenuItem 列表,然后将其发送到下拉小部件

例如,在下面的代码中,provinces 是一个列表,provisionsItems 是一个 dropdownMenueItem 的列表,我用我想要从省份列表中获取的项目来填充 prosesItems,然后将其发送到 dropDown Widget

for (int i = 0; i < provinces.length; i++) {
      provincesItems.add(DropdownMenuItem(
        value: i,
        child: Column(
          children: [
            Text(
              provinces[i].persianName,
              style: TextStyle(
                  color: Colors.black, fontWeight: FontWeight.normal),
            ),
          ],
        ),
      ));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多