gpt4 book ai didi

dart - Flutter DropdownButton 失败

转载 作者:IT王子 更新时间:2023-10-29 07:00:51 28 4
gpt4 key购买 nike

我在 appBar 和脚手架主体中创建了两个 DropdownButton。只要选择顶部 DropdownButton 就会更改语言。在我从第二个 Dropdown Button 中选择 One Conversion rate 并更改语言后,代码崩溃了。这是代码..

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class User {
User(this.name);
final String name;
}

class CurrencyClass {
final String currencyType;
CurrencyClass(this.currencyType);
}
class _MyHomePageState extends State<MyHomePage> {

User selectedUser;
CurrencyClass selectedCurrency;
List<User> users = <User>
[User('English'),User('Amharic'),User('Oromigna'),
User('Tigrigna'),User('Somali')];
List<CurrencyClass> currency = <CurrencyClass>[
CurrencyClass('USD'),
CurrencyClass('GBP'),
CurrencyClass('CAD'),
CurrencyClass('AUD'),
CurrencyClass('EURO'),CurrencyClass('SAR'),];

@override
void initState() {
super.initState();
selectedUser = users[0];
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
actions: <Widget>[
DropdownButton<User>(
value: selectedUser,

onChanged:(User newValue){
setState(() {
selectedUser = newValue;

if(newValue == users[0]){

currency = <CurrencyClass>[CurrencyClass('English'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
else if(newValue == users[1]){
currency = <CurrencyClass>[CurrencyClass('Amharic'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}

else if(newValue == users[2]){
currency = <CurrencyClass>[CurrencyClass('Oromigna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

} else if(newValue == users[3]){
currency = <CurrencyClass>[CurrencyClass('Tigringa'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

} else {
currency = <CurrencyClass>[CurrencyClass('Somaligna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

}

});
},
items: users.map((User user){
return new DropdownMenuItem<User>(
value: user,
child:Text(
user.name,
style: TextStyle(color: Colors.black),
),
);
}).toList(),
)
],
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Padding(padding: EdgeInsets.symmetric(vertical: 50.0),),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Amount',style: TextStyle(fontSize: 20.0),),
Padding(padding: EdgeInsets.symmetric(horizontal: 15.0)),
Text('Birr',style: TextStyle(fontSize: 24.0),),
Padding(padding: EdgeInsets.symmetric(horizontal: 15.0)),
DropdownButton<CurrencyClass>(
value: selectedCurrency,
onChanged: (CurrencyClass newExchange){
setState(() {
selectedCurrency = newExchange;
});
},
items: currency.map((CurrencyClass user2){
return DropdownMenuItem<CurrencyClass>(
value: user2,
child: Text(
user2.currencyType,
style: TextStyle(color: Colors.red),
),
);
}).toList(),
),
],
),
],
),
),
);
}
}

这是我在底部下拉按钮中选择某个值后选择顶部下拉按钮时的错误

Launching lib/main.dart on XT1068 in debug mode...
Built build/app/outputs/apk/debug/app-debug.apk.
I/FlutterActivityDelegate(11200): onResume setting current activity to
this
I/flutter (11200): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter (11200): The following assertion was thrown building
MyHomePage(dirty, state: _MyHomePageState#3ce11):
I/flutter (11200): 'package:flutter/src/material/dropdown.dart': F
Failed assertion: line 481 pos 15: 'value == null ||
I/flutter (11200): items.where((DropdownMenuItem<T> item) =>
item.value == value).length == 1': is not true.
I/flutter (11200): Either the assertion indicates an error in the
framework itself, or we should provide substantially
I/flutter (11200): more information in this error message to help you
determine and fix the underlying cause.

最佳答案

原始帖子中的问题是在选择条形下拉菜单时设置货币值。您还需要设置 selectedCurrency

selectedCurrency = currency[0];

或者像 Gunter 在编辑中所做的那样。只是不要更改货币下拉列表,所以根本没有问题。

    onChanged:(User newValue){
setState(() {
selectedUser = newValue;

if(newValue == users[0]){
currency = <CurrencyClass>[CurrencyClass('English'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
else if(newValue == users[1]){
currency = <CurrencyClass>[CurrencyClass('Amharic'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
else if(newValue == users[2]){
currency = <CurrencyClass>[CurrencyClass('Oromigna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
else if(newValue == users[3]){
currency = <CurrencyClass>[CurrencyClass('Tigringa'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
else {
currency = <CurrencyClass>[CurrencyClass('Somaligna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
}
selectedCurrency = currency[0];
});
},

关于dart - Flutter DropdownButton 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50840050/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com