'Flutter : Dynamic DropDownButton get error (one item with [DropdownButton]'s value)

I have this dropdownbutton which get values from provider list as shown :

Consumer<OrderProvider>(builder: (context, orderProv, child) {
    print(orderProv.sizeList);
    return Container(
        width: 100,
        child: DropdownButton(
            isExpanded: true,
            value: orderProv.size,
            onChanged: (val) {
                orderProv.changeSize(val);
            },
            items: orderProv
                     .sizeList
                     .map<DropdownMenuItem<String>>((e) {
                          return DropdownMenuItem<String>(
                              value: e,
                              child: Text(e),
                          );
                      }).toList()
        ),
    );
});

At the second line print(orderProv.sizeList), when I print the dropdown menu list it prints the list correctly:

[S, M, L, XL]

But I receive the following error :

There should be exactly one item with [DropdownButton]'s value: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value



Solution 1:[1]

The value provided to the DropDownButton must be one of the values in the DropdownMenuItem. Please ensure that the orderProv.size points to one of the elements in the orderProv.sizeList.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Swanav