【发布时间】:2021-11-15 12:24:47
【问题描述】:
我目前正在构建一个将货币列表返回到下拉菜单的 botcoin 代码应用程序。 我收到以下错误:-
错误:主体可能正常完成,导致返回“null”,但返回类型可能是不可为空的类型。 (body_might_complete_normally at [bitcoin_ticker] lib\price_screen.dart:10)
错误:不能将参数类型“List
这是我的代码
import 'package:flutter/material.dart';
import 'coin_data.dart';
int i = 0;
class PriceScreen extends StatefulWidget {
@override
_PriceScreenState createState() => _PriceScreenState();
}
List<DropdownMenuItem> getDropDownItems() {
List<DropdownMenuItem<String>> dropdownItem = [];
for (String currency in currenciesList) {
DropdownMenuItem(
child: Text(currency), //
value: currency,
);
}
}
class _PriceScreenState extends State<PriceScreen> {
late String selectedCurrency = 'USD';
@override
Widget build(BuildContext context) {
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,//Default value
items: getDropDownItems(),
onChanged:(value){//it is like an on Pressed Button
setState(() {
selectedCurrency = value!;
print(selectedCurrency);
});
},
),
),
],
),
);
}
}
【问题讨论】:
-
您的
getDropDownItems函数缺少return语句,因此不会返回任何内容。此外,它的for-loop 主体不会对其构造的DropdownMenuItem对象做任何事情。 -
@jamesdlin 我编写了 for 循环来访问列表并将其打印在下拉列表中..你能告诉我如何更改 for 循环
标签: android flutter android-studio dart non-nullable