'The argument type 'List<DropdownMenuItem<dynamic>>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?
Currently, I am working on a Flutter tutorial which was developed on previous versions. The error occurred once we updated to the latest version of Dart and Flutter, probably due to null safety.
import 'package:bitcoin_ticker/coin_data.dart';
import 'package:flutter/material.dart';
class PriceScreen extends StatefulWidget {
@override
_PriceScreenState createState() => _PriceScreenState();
}
class _PriceScreenState extends State<PriceScreen> {
String? selectedCurrency = 'USD';
List<DropdownMenuItem> getDropdownItems(){
List<DropdownMenuItem<String>> dropdownItems = [];
for (String currency in currenciesList){
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
}
return dropdownItems;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('🤑 Coin Ticker'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
height: 150.0,
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: DropdownButton<String>(
value: selectedCurrency,
items: getDropdownItems(), // This is where I'm getting an error
onChanged: (value){
setState(() {
selectedCurrency = value;
});;
}),
),
],
),
);
}
}
I am a beginner to flutter and Dart so any help is appreciated.
Solution 1:[1]
Just remove <String>
from DropdownButton<String>
child: DropdownButton(
value: selectedCurrency,
items: getDropdownItems(), // This is where I'm getting an error
onChanged: (value){
setState(() {
selectedCurrency = value;
});;
}),
Solution 2:[2]
You should change this line
List<DropdownMenuItem> getDropdownItems(){
to this
List<DropdownMenuItem<String>> getDropdownItems(){
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LekPKD |
Solution 2 | manhtuan21 |