0

I am trying to implement a simple DropdownButton with which the user can select a currency.

 Container(
        height: 150.0,
        alignment: Alignment.center,
        padding: EdgeInsets.only(bottom: 30.0),
        color: Colors.lightBlue,
        child: DropdownButton<String>(
          items: [
            DropdownMenuItem(child: Text('USD')),
            DropdownMenuItem(child: Text('EUR')),
            DropdownMenuItem(child: Text('GBP')),
          ],
          value: 'USD',
          onChanged: (value) {
            print(value);
          },
        ),
      ),

But i am getting below error

Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

How to overcome this error? Thanks in advance

0

1 Answer 1

1

As per the Documentation here

Your DropdownButton items expects a DropdownMenuItem with both child and value

So Use DropdownMenuItem as

items: [
            DropdownMenuItem(child: Text('USD'), value: 'USD'),
            DropdownMenuItem(child: Text('EUR'), value: 'EUR'),
            DropdownMenuItem(child: Text('GBP'), value: 'GBP'),
          ],

For dynamic dropdown use it as.

  var dropdownvalue = 'USD';

 items: <String>['USD', 'EUR', 'GPB']
              .map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
          value: dropdownvalue,
          onChanged: (value) {
            setState(() {
              dropdownvalue = value;
            });
          },

Hope it solves!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.