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

后端 未结 10 1386
借酒劲吻你
借酒劲吻你 2021-01-17 13:07

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 th

相关标签:
10条回答
  • 2021-01-17 13:27

    You can use the same list in multiple DropDownButton. The error you got is because of having more than one same values in the list. For Example, if I change the list to given below where I have two items having the same value, it will throw me an error.

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

    Error:

    0 讨论(0)
  • 2021-01-17 13:34

    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(), 
    );
    
    0 讨论(0)
  • 2021-01-17 13:35

    You must initialise the _value1 and _value2 with a initial Value.

    0 讨论(0)
  • 2021-01-17 13:38

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

    0 讨论(0)
  • 2021-01-17 13:38

    _value1 and _value2 must be in your list

    0 讨论(0)
  • 2021-01-17 13:39

    I have the same problem, and I solved it. The dropdown button needs items list and value. We define items and selected items, but the item chosen instance does not inside the items list.

    You should try this and fix your logic. (value ıs selected item value for user)

     var _value = itemList.isEmpty
        ? value
        : itemList.firstWhere((item) => item.value == value.value);
    

    More : https://gist.github.com/VB10/fd560694fec0a38751e798a213408001

    0 讨论(0)
提交回复
热议问题