简体   繁体   English

无法在 flutter 中实现 DropdownButton

[英]Unable to implement DropdownButton in flutter

I am trying to implement a simple DropdownButton with which the user can select a currency.我正在尝试实现一个简单的DropdownButton ,用户可以使用它 select 货币。

 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提前致谢

As per the Documentation here根据此处文档

Your DropdownButton items expects a DropdownMenuItem with both child and value您的DropdownButton项需要具有子项和值的 DropdownMenuItem

So Use DropdownMenuItem as所以使用 DropdownMenuItem 作为

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!希望能解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM