I am currently learning Flutter and want to create a DropdownButton with the following list:
const List<String> currenciesList = [
'AUD',
'BRL',
'CAD',
'CNY',
'EUR',
];
The item attribute of the DropdownButton widget expects a list. Therefore, I want to clean the code by using a list comprehension. I have written the following function to create a DropdownMenuItem depending on the given string:
DropdownMenuItem makeDropdownMenuItem (String currency) {
DropdownMenuItem item = DropdownMenuItem(
child: Text(currency),
value: currency,
);
return item;
}
Unfortunately, when I use the list comprehension syntax I found here, I get an error:
child: DropdownButton<String>(
value: selectedCurrency,
items: currenciesList.map((currency) => makeDropdownMenuItem(currency)).toList(),
onChanged: (value) {
setState(() {
selectedCurrency = value;
});
},
type 'List<DropdownMenuItem>' is not a subtype of type 'List<DropdownMenuItem>?'
Can anyone help me fixing this please? Thanks a lot!