当我将'value'参数添加到DropdownButton时,我的应用程序崩溃了,但标题中提到了例外情况。 当我删除“值”参数时,它起作用了,但显示了提示;当我从列表中选择一个值时,它并没有真正被选择,我的意思是我看不到我选择了它。
Widget build(BuildContext context) {
String dropdownValue = 'Categories';
return Scaffold(
appBar: AppBar(
title: Text(
'Podium',
style: TextStyle(fontSize: 18),
),
backgroundColor: Colors.black54,
),
backgroundColor: Colors.grey,
body: FutureBuilder<Competition>(
future: widget.jsonData.fetchComp(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Center(
child: Column(
children: <Widget>[
Text(
snapshot.data.compName,
style: TextStyle(fontSize: 18),
),
DropdownButton<String>(
items:
snapshot.data.categoriesNames.map((dropdownStringItem) {
return DropdownMenuItem<String>(
value: dropdownStringItem,
child: Text(dropdownStringItem),
);
}).toList(),
onChanged: (newItemSelected) {
setState(() {
dropdownValue = newItemSelected;
});
},
value: dropdownValue,
elevation: 16,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
style: TextStyle(color: Colors.black54),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
)
],
));
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return Center(
child: CircularProgressIndicator(),
);
},
));
}
答案 0 :(得分:0)
Widget build(BuildContext context) {
String dropdownValue = null;
return Scaffold(
appBar: AppBar(
title: Text(
'Podium',
style: TextStyle(fontSize: 18),
),
backgroundColor: Colors.black54,
),
backgroundColor: Colors.grey,
body: FutureBuilder<Competition>(
future: widget.jsonData.fetchComp(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Center(
child: Column(
children: <Widget>[
Text(
snapshot.data.compName,
style: TextStyle(fontSize: 18),
),
DropdownButton<String>(
items:
snapshot.data.categoriesNames.map((dropdownStringItem) {
return DropdownMenuItem<String>(
value: dropdownStringItem,
child: Text(dropdownStringItem),
);
}).toList(),
onChanged: (newItemSelected) {
setState(() {
dropdownValue = newItemSelected;
});
},
value: dropdownValue,
elevation: 16,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
style: TextStyle(color: Colors.black54),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
)
],
));
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return Center(
child: CircularProgressIndicator(),
);
},
));
}
下拉值的开头或“ items”属性项应为null。
e.g.
String dropdownValue = null;