Abdullah
Abdullah

Reputation: 567

Errror occured in flutter DropdownButton

Exception has occurred. _AssertionError ('package: flutter/src/material/dropdown.dart': Failed assertion: line 915 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1': There should be exactly one item with [DropdownButton]'s value: 0. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)

Upvotes: 0

Views: 3431

Answers (2)

Abdullah
Abdullah

Reputation: 567

Your "value" for DropdownButton should be set to 'null' or or be one from the values list.

DropdownButton(
      value: null,
      isDense: true,
      onChanged: (String newValue) {
        // somehow set here selected 'value' above whith 
       // newValue
       // via setState or reactive.
      },
      items: ['yellow', 'brown', 'silver'].map((String value) {
        return DropdownMenuItem(
          value: value,
          child: Text(value),
        );
      }).toList(),
    ),

Upvotes: 6

Jignesh Patel
Jignesh Patel

Reputation: 223

Initialize the value object with some default value. Please note that the value should be one of the values contained by your collection.

String selectedArray = "ABC";  //This is the selection value. It is also present in my array.
  final array = [“ABC”, “DEF”, “GHI”, “JKL”];  //This is the array for dropdown

Upvotes: 1

Related Questions