Widget _buildDropDownButton(String currencyCategory) {
return DropdownButton(
value: currencyCategory,
items: currencies
.map((String value) => DropdownMenuItem(
value: value,
child: Row(
children: <Widget>[
Text(value),
],
),
))
.toList(),
onChanged: (String value) {
if(currencyCategory == fromCurrency){
_onFromChanged(value);
}else {
_onToChanged(value);
}
},
);
}
嗨,我在这部分示例代码中有一个问题,在这部分代码中:
onChanged: (String value) {
if(currencyCategory == fromCurrency){
_onFromChanged(value);
}else {
_onToChanged(value);
}
},
显示错误The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'
我尝试放置final void Function() onChanged;
或final VoidCallback onChanged;
,但它期望和标识符。我错过了什么吗?
这是因为null安全性,当没有选择任何内容时,下拉菜单的值可以为null。加上"?"之后的String类型声明要像onChanged(String? value) {//on change logic}
。
查看flutter DropdownButton示例。