flutter - 从 DropdownItems 中选择值后,DropdownButton 值未更新。如何使用 selectedLocation 更新默认值?
问题描述
从 DropdownItems 中选择值后,DropdownButton 值未更新。如何使用 _selectedLocation 更新默认值?
我使用的变量:
String? stateToCountry;
List? stateID;
List? cityJson;
Position? _position;
String? currentAddress;
Placemark? place;
DropdownButton(
hint:_selectedLocation==null ? Text('Dropdown') : Text(
_selectedLocation.toString(),
style: TextStyle(color: Colors.black),
),
isExpanded: true,
iconSize: 30,
elevation: 16,
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
style: TextStyle(color: Colors.deepPurple, fontSize: 20.0),
itemHeight: 50,
items: countrylist!.map((location){
return DropdownMenuItem(
value: location,
child: new Text(location["name"].toString()),
);
}).toList(),
onChanged: (newValue){
setState(() {
_selectedLocation=newValue as String?;
});
},
),
解决方案
您需要设置 value 属性,
DropdownButton(
value: _selectedLocation,
onChanged:(newValue){
setState(() {
_selectedLocation=newValue;
});
}
推荐阅读
- ios - 如何为 iOS 平台的 g-streamer 中的特定元素启用调试?
- hash - 我的 redis “ERR 未知命令 'HSTRLEN'”怎么了?
- javascript - 如何从循环数据数组中使用ajax发送参数
- mysql - 触发器不适用于 MySql
- unit-testing - 单元测试 + 工作单元 + 存储库
- java - 比较字符串与 Cp1252 编码
- php - 如何发送下一个 GET 请求?
- android - 如何管理不同的按钮来打开片段?
- javascript - 如何使用 js 在 twilio 客户端中获取通话的活动通话时间?
- sockets - 在套接字编程中通常只允许使用每个套接字地址(协议/网络地址/端口)