[英]Making a DropDownButton of CheckBoxListTile in flutter
[英]Not able to select item of Flutter Dropdownbutton
我试图在颤振项目中实现下拉按钮。下拉按钮列表将从 api 填充。 我已经从 api 中获取了列表。
但是当我选择一个项目时,它没有显示。 错误信息如下
The following assertion was thrown building FutureBuilder<List<Categories>>(dirty, state: _FutureBuilderState<List<Categories>>#09534):
There should be exactly one item with [DropdownButton]'s value: Instance of 'Categories'.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null items.isEmpty value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
我还在 Pastebin 上添加下拉按钮代码。
我认为在你的情况下value: _currentItemSelected
可能为空
您还应该管理FutureBuilder snapshotstate and error
String _currentItemSelected = "";
FutureBuilder<List<Categories>>(
future: _fetchCats(),
builder: (BuildContext context,
AsyncSnapshot<List<Categories>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error,
),
);
} else {
return DropdownButton<String>(
isExpanded: true,
items: snapshot.data?.map((Categories value) {
return new DropdownMenuItem<String>(
value: value.name,
child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
);
})?.toList() ?? [],
onChanged: (String value) {
//field.didChange(value.name);
setState(() {
print(value);
this._currentItemSelected = value;
});
},
value: _currentItemSelected == "" ? _currentItemSelected = snapshot.data.first.name : _currentItemSelected,,
hint: Text('Event Category',
style: TextStyle(
fontSize: 15,
color: Colors.black,
),
),
underline: Container(
decoration: const BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey))
),
),
style: TextStyle(
fontSize: 15,
color: Color(0xffF9B538),
),
);
})
}
希望能帮助到你..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.