FullCurrencyList
我在一个单独的文件中有一个类currencies.dart
,我尝试在第二个页面上导入它,然后构建一个 DropdownMenuItem 小部件。
现在我收到以下错误:
The argument type 'List<DropdownMenuItem<FullCurrencyList>>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?'
class FullCurrencyList {
static final List<Map<String, dynamic>> currencies = <Map<String, dynamic>>[
<String, dynamic>{
"code": "USD",
"name": "United States Dollar",
}
];
}
这是我的完整代码:
import 'package:flutter/material.dart';
import 'package:brain/util/currencies.dart';
class Page3 extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _Page3State();
}
class _Page3State extends State<Page3> {
String? _valCurrency;
List<Map<String, dynamic>> _currencyList = FullCurrencyList.currencies;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
child: Text(("Please, select your currency:"),
),
),
new Row(
children: <Widget>[
Container(
child: Text(("Currency") + ":",
)
),
Container(
child: DropdownButton<String>(
borderRadius:BorderRadius.circular(10),
hint: Text("Select"),
value: _valCurrency,
items: _currencyList.map<DropdownMenuItem<FullCurrencyList>>((FullCurrencyList value) {
return DropdownMenuItem<FullCurrencyList>(
value: value,
child: Text(value.name + ' ' + value.code),
);
}).toList(),
onChanged: (String? value) {
setState(() {
_valCurrency = value!;
});
},
),
),
]),
]);
}
}
您的货币列表不是 类型
FullCurrencyList
,而是 类型List<Map<String, dynamic>>
。仅仅因为您这样命名类并不意味着它currencies
属于该类型。我假设您希望代码实际上是值,那么您可以将参数替换items
为