【问题标题】:How can i set value to dropdown in flutter?如何在颤动中将值设置为下拉列表?
【发布时间】:2021-04-04 09:58:28
【问题描述】:

我有一个用Satuan 调用的类,现在我想为其设置值,然后在我的下拉列表中将其用作选定值。这就是我的工作

 Satuan selectedUom;
 Future<String> get_itemuom() async {
    var res = await widget.itemCategoryRepository.get_uom();
    setState(() {
      res.forEach((element) {
        if (element.uomId == widget.item.uomId) {
          selectedUom = Satuan(
              uomId: element.uomId,
              uomName: element.uomId,
              flagDecimal: element.flagDecimal);
        }
      });
      itemsuom = res
          .map(
            (uom) => DropdownMenuItem(
              child: Text(uom.uomName),
              value: uom,
            ),
          )
          .toList();
    });
    return "Sucess";
  }

这是我的下拉菜单

   Widget satuan_field() {
return (itemsuom.length < 1)
    ? Center(
        child: CircularProgressIndicator(),
      )
    : Padding(
        padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
        child: Container(
          color: Color(0xfff5f5f5),
          child: new DropdownButtonFormField<Satuan>(
            validator: (value) {
              if (value == null) {
                return 'Wajib di isi';
              }
              return null;
            },
            style: TextStyle(color: ColorThemes().textColor),
            decoration: InputDecoration(
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(25.0),
                  borderSide: BorderSide(
                    color: ColorThemes().mainColor,
                    width: 2.0,
                  ),
                ),
                border: OutlineInputBorder(),
                labelText: 'Satuan',
                prefixIcon: Icon(Icons.card_membership,
                    color: ColorThemes().secondaryColor),
                labelStyle: TextStyle(
                    fontSize: ColorThemes().default_text_size,
                    color: ColorThemes().secondaryColor)),
            items: itemsuom,
            value: selectedUom,
            onChanged: (value) {
              setState(() {
                _mySelectionuom = value.uomId;
                isInputDecimal = value.flagDecimal;
                _itemStock.text = "";
              });
            },
          ),
        ),
      );

}

当我运行它时,我得到了这个错误

I/flutter (6652): ══╡ 小部件库发现的异常 ╞═════════════════════════════════════════════════ ══════════ I/颤振 (6652):在构建 EditScreen(脏, state: _EditScreenState#82e0b): I/flutter (6652): 应该有 正是一项具有 [DropdownButton] 值的项目:“Satuan”的实例。 I/flutter(6652):零个或 2 个或更多 [DropdownMenuItem] 是 以相同的值 I/flutter 检测到(6652): 'package:flutter/src/material/dropdown.dart': I/flutter (6652): 断言失败:第 1478 行 pos 15:'items == null ||项目.isEmpty || 值 == 空 ||我/颤振(6652):
items.where((DropdownMenuItem item) { I/flutter (6652):
返回 item.value == 值;我/颤振(6652):})。长度 == 1'

我该如何解决?提前致谢

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    此行返回重复项,因此下拉菜单无法确定哪个是哪个。

    var res = await widget.itemCategoryRepository.get_uom();
    

    您是否使用 FutureBuilder 读取数据?

    【讨论】:

    • 它返回 5 个项目,正如预期的那样
    • 什么是 Satuan 的结构
    • 它正在返回重复的项目。你是对的,也很抱歉。我已经通过添加 @override bool operator ==(other) { return this.uomId == other.uomId; } 来修复它
    • 您还对哈希值进行了覆盖?这是覆盖 == 的合同的一部分。
    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2020-06-07
    • 2017-05-31
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多