Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value I/flutter (18363): 'package:flutter/src/material/dropdown.dart':

Tags:

flutter

Error code Hi I'm new to flutter and have a question about dropdownbutton regarding using the same values for multiple dropdownbutton.

From my understanding from the error, it was due to using the same list for 2 or more dropdownbuttons in the same activity.

How am i able to resolve this error but still able to reuse the list for 2 or more dropdownbuttons?

  String _value1;
  String _value2;

  final List<String> nameList = <String>[
    "Name1",
    "Name2",
    "Name3",
    "Name4",
    "Name5",
    "Name6",
    "Name7",
    "Name8"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 2.0,
        title: Text('Hello'),
      ),
      body:  ListView(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value1,
                      onChanged: (value) {
                        setState(() {
                          _value1 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value2,
                      onChanged: (value) {
                        setState(() {
                          _value2 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
            ],
          ),
      backgroundColor: Colors.grey[200],
    );
  }
}
like image 271
Wei Jun Avatar asked Dec 25 '19 00:12

Wei Jun


People also ask

How do I style a DropDown button in flutter?

If we want to display some other text instead of the selected option on the button we will use selectedItemBuilder. DropdownButton( value: _value, selectedItemBuilder: (BuildContext context) { return list_items. map<Widget>((int item) { return Text('item $item'); }). toList(); }, items: list_items.

How do I open DropDown dialog below DropDownButton?

Option 1: Set DropDown. dart selectedItemOffset to -40 in then DropDownItems will always opens below the DropdownButton .


4 Answers

I had the exact same error, multiple Dropdowns all feeding from the same static list, the only difference is that in my case, it was a list of Objects, not Strings.

So, if it's a static list, there's no way it's empty, no duplicate values in the list, AND you already make sure value is not empty? Then the only option remaining is that item.value is different than value

In my case, as it was an Object list, I had to overwrite operator == and hashcode methods in my Object class.

bool operator ==(dynamic other) =>       other != null && other is TimeSelection && this.hour == other.hour;    @override   int get hashCode => super.hashCode; 

And that was it. I didn't had to initialize _value1 or _value2

like image 145
David_E Avatar answered Sep 21 '22 18:09

David_E


This exception you have because of mistakes:

  1. No _value1 and _value2 initialization.
  2. When you initialize them make sure that _value1 and _value2 right from nameList e.g.
_value1 = nameList[0]; _value2 = nameList[3]; 

this is important step with complex data type, but in your case

_value1 = "Name1"; _value2 = "Name4"; 

will be sufficient.

Full example:

  String _value1;   String _value2;    final List<String> nameList = <String>[     "Name1",     "Name2",     "Name3",     "Name4",     "Name5",     "Name6",     "Name7",     "Name8"   ];    /// initialization is here:   @override  void initState() {     super.initState();     _value1 = nameList[0];     _value2 = nameList[3];   }    @override   Widget build(BuildContext context) {     return ListView(       children: <Widget>[         Row(           children: <Widget>[             Text('Name: '),             Center(               child: DropdownButton(                 value: _value1,                 onChanged: (value) {                   setState(() {                     _value1 = value;                   });                 },                 items: nameList.map(                   (item) {                     return DropdownMenuItem(                       value: item,                       child: new Text(item),                     );                   },                 ).toList(),               ),             ),           ],         ),         Row(           children: <Widget>[             Text('Name: '),             Center(               child: DropdownButton(                 value: _value2,                 onChanged: (value) {                   setState(() {                     _value2 = value;                   });                 },                 items: nameList.map(                   (item) {                     return DropdownMenuItem(                       value: item,                       child: new Text(item),                     );                   },                 ).toList(),               ),             ),           ],         ),       ],     );   } } 
like image 44
Richard Sipher Avatar answered Sep 20 '22 18:09

Richard Sipher


You are getting that exception because _value1 and _value2 aren't initialized and providing empty to the dropdown widget.

You could do something like this:

DropdownButton(
  value: _value1.isNotEmpty ? _value1 : null, // guard it with null if empty
  items: nameList.map((item) {
           return DropdownMenuItem(
              value: item,
              child: new Text(item),
           );
         }).toList(), 
);
like image 25
Mithun Nath Avatar answered Sep 23 '22 18:09

Mithun Nath


You must initialise the _value1 and _value2 and make sure those values are also present in nameList.

like image 29
Anuroop Singh Avatar answered Sep 21 '22 18:09

Anuroop Singh