RangeError(索引):无效值:不在包含范围 0..2:3 内
RangeError (index) : invalid calue : not in incusive range 0..2:3
嘿伙计们,我正在尝试制作一个应用程序,以示例方式跟踪比特币价格......但它在 android-emulator 中给了我错误,但代码本身没有任何错误,我不知道出了什么问题,
我认为问题出在 DropdownMenuItem 列表中,但我无法获取它
谁能帮忙,谢谢
class PriceScreen extends StatefulWidget {
@override
_PriceScreenState createState() => _PriceScreenState();
}
class _PriceScreenState extends State<PriceScreen> {
String selectedCurrency = 'EGP';
List<DropdownMenuItem<String>> getDropdownItems() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (int i = 0; i < currenciesList.length; i++) {
String currency = (cryptoList[i]);
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
}
return dropdownItems;
}
@override
Widget build(BuildContext context) {
getDropdownItems();
return Scaffold(
appBar: AppBar(
title: Text(' Coin Ticker'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
child: Card(
color: Colors.lightBlueAccent,
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
child: Text(
'1 BTC = ? USD',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
Container(
height: 150.0,
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: DropdownButton<String>(
value: selectedCurrency,
items: getDropdownItems(),
onChanged: (value) {
setState(() {
selectedCurrency = value!;
});
}),
),
],
),
);
}
}
android 模拟器:
for
循环应该检查您正在访问的数组的长度。
因此,它应该是这个
for (int i = 0; i < currenciesList.length; i++) {
String currency = (currenciesList[i]);
}
或
for (int i = 0; i < cryptoList.length; i++) {
String currency = (cryptoList[i]);
}
嘿伙计们,我正在尝试制作一个应用程序,以示例方式跟踪比特币价格......但它在 android-emulator 中给了我错误,但代码本身没有任何错误,我不知道出了什么问题,
我认为问题出在 DropdownMenuItem 列表中,但我无法获取它
谁能帮忙,谢谢
class PriceScreen extends StatefulWidget {
@override
_PriceScreenState createState() => _PriceScreenState();
}
class _PriceScreenState extends State<PriceScreen> {
String selectedCurrency = 'EGP';
List<DropdownMenuItem<String>> getDropdownItems() {
List<DropdownMenuItem<String>> dropdownItems = [];
for (int i = 0; i < currenciesList.length; i++) {
String currency = (cryptoList[i]);
var newItem = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropdownItems.add(newItem);
}
return dropdownItems;
}
@override
Widget build(BuildContext context) {
getDropdownItems();
return Scaffold(
appBar: AppBar(
title: Text(' Coin Ticker'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
child: Card(
color: Colors.lightBlueAccent,
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
child: Text(
'1 BTC = ? USD',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
Container(
height: 150.0,
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 30.0),
color: Colors.lightBlue,
child: DropdownButton<String>(
value: selectedCurrency,
items: getDropdownItems(),
onChanged: (value) {
setState(() {
selectedCurrency = value!;
});
}),
),
],
),
);
}
}
android 模拟器:
for
循环应该检查您正在访问的数组的长度。
因此,它应该是这个
for (int i = 0; i < currenciesList.length; i++) {
String currency = (currenciesList[i]);
}
或
for (int i = 0; i < cryptoList.length; i++) {
String currency = (cryptoList[i]);
}