El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

Flutter - Dropdown value

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();   

Upvotes: 6

Views: 25817

Answers (6)

Abdelkhalek Haddany
Abdelkhalek Haddany

Reputation: 453

for me i just initialize selectedValue with first element in the list as you see:

  @override
  Widget build(BuildContext context) {

      String selectedValue = list.first;

       ....

Upvotes: 0

Abdul Moeed
Abdul Moeed

Reputation: 41

The DropdownButton widget has the following assertion

assert(items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1,
                "There should be exactly one item with [DropdownButton]'s value: "
                '$value. \n'
                'Either zero or 2 or more [DropdownMenuItem]s were detected '
                'with the same value',
              )

In this assertion the following part fails and returns empty list

items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              })

In my case i was using an object of a class that contained several other properties I override the == operator for that class and it worked! Following is an example of operator overriding

class Person {
  String name;
  int id;

  Person(this.name, this.id);

  bool operator ==(o) => o is Person && o.name == name && o.id == id;
}

Upvotes: 4

Denn
Denn

Reputation: 823

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(),
);

Upvotes: 12

Rodrigo
Rodrigo

Reputation: 455

Here works fine,

StreamBuilder(
    stream: bloc.selecionado$,
    builder: (context, snapshot) {
      if(snapshot.hasData){
      return DropdownButton(
        items: bloc.listAtletas.value.map( (v){
          return DropdownMenuItem<AtletasObj>(
            child: Text("${v.nome}"),
            value: v,
          );
        }).toList(),
        value: bloc.selecionado.value,
        isDense: true,
        onChanged:(v){bloc.changeAtletaSelecionado(v);                                                                                                              bloc.changeObjetoSelecionadoFunction(v);},
      );
      }
      return Container(height: 0.0, width: 0.0,);
    }
  ),

Upvotes: 0

GirlWhoCode
GirlWhoCode

Reputation: 628

try this hope its works for you :

 new DropdownButton(
                            value: _current,
    items: dataMaker.map((item) {
                              return new DropdownMenuItem<String>(
                                child: new Text(item.fabricante,
                                    textAlign: TextAlign.center),
                                value: item.fabricante, //FAIL
                              );
                            }).toList(),
                            onChanged: (selected) => setState(() {
      _current = selected;
    });

Upvotes: 2

Devarsh Ranpara
Devarsh Ranpara

Reputation: 1112

You can try this method, Thats how i use sometimes.

Code may contain some problem bcz i have created it in normal TextEditor.

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;
}

Upvotes: -2

Related Questions