Flutter - 下拉值

El *_*bre -1 dropdown flutter

我需要在颤振下拉列表中使用字符串作为值。但返回

抛出了另一个异常:'package:flutter/src/material/dropdown.dart': Failed assertion: line 609 pos 15: 'items == null || items.isEmpty || 值 == 空 || items.where((DropdownMenuItem item) => item.value == value).length == 1': 不是真的。

完整日志在这里

代码是

items: dataMaker.map((item) {
                          return new DropdownMenuItem<String>(
                            child: new Text(item['fabricante'],
                                textAlign: TextAlign.center),
                            value: item['fabricante'], //FAIL
                          );
                        }).toList(),
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:如何使用字符串作为项目值?另一种解决方案是获取下拉文本,但我不知道如何。

- 解决方案 -

使用此代码,我可以找到 dataMaker 列表中与下拉菜单具有相同文本的所有元素。

 var test =dataMaker.firstWhere((fabricante) => fabricante["id"].toString() == dropdownSelectionMaker);              
 dataModelo = dataMaker.where((modelo) => modelo["fabricante"] == test["fabricante"]).toList();   
Run Code Online (Sandbox Code Playgroud)

Den*_*enn 6

我不确定库是如何进行比较的,但我发现尽管该值在他的列表中,但它仍然返回错误,因此我不得不进行一些手动比较。注意:BuildingType只是一种自定义数据类型。这是我的案例的神奇之处: value: selectedType == null ? selectedType : buildingTypes.where( (i) => i.name == selectedType.name).first as BuildingType,

return DropdownButton<T>(
  hint: Text("Select Building type"),
  value: selectedType == null ? selectedType : buildingTypes.where( (i) => i.name == selectedType.name).first as BuildingType,
  onChanged: handleBuildingTypeChange,
  items: abcList.map((T value) {
    return DropdownMenuItem<T>(
      value: value,
      child: Row(
        children: <Widget>[
          SizedBox(
            width: 10,
          ),
          Text(
            value.name,
            style: TextStyle(color: Colors.black),
          ),
        ],
      ),
    );
  }).toList(),
);
Run Code Online (Sandbox Code Playgroud)


Dev*_*ara -2

你可以试试这个方法,我有时就是这么用的。

代码可能包含一些问题,因为我在普通文本编辑器中创建了它。

items: (){
    List<Widget> data = List<Widget>();
    for(int i=0;i<dataMaker.length;i++){
        data.add(
            DropdownMenuItem<String>(
                child: Text(item['fabricante'],
                textAlign: TextAlign.center),
                value: item['fabricante'],
            );
        );
    }
    return data;
}
Run Code Online (Sandbox Code Playgroud)