目前,我正在做一个在以前版本上开发的Flutter教程。错误发生在我们更新到最新版本的Dart和Flutter之后,可能是由于零安全。
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;
});;
}),
),
],
),
);
}
}
我是一个初学者,以Flutter和飞镖,所以任何帮助是赞赏。
2条答案
按热度按时间rta7y2nd1#
只需从
DropdownButton<String>
中删除<String>
v2g6jxz62#
您应该更改此行
对此