Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need use a string as a value in the flutter dropdown. But returns

Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 609 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.

Full log here.

The code is

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

So my question in: How can i use a string as item value? Another solution is get the text of dropdown but i don′t know how.

--SOLUTION--

With this code I can find all the elements in the dataMaker list that have the same text as the drop-down menu.

 var test =dataMaker.firstWhere((fabricante) => fabricante["id"].toString() == dropdownSelectionMaker);              
 dataModelo = dataMaker.where((modelo) => modelo["fabricante"] == test["fabricante"]).toList();   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm not sure how the library does the comparison but I found that though the value was in he list it still returned the error, so I had to do some manual comparison. NOTE: BuildingType is just a custom data type. Here's the magical line for my case: 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(),
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...