我有一个下拉列表,列表项是从webapi获取的。 有时获取json需要一段时间,并且dropdownlist无法成功绑定。引发异常:
I/flutter (21159): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (21159): The following assertion was thrown building ScopedModelDescendant<MainModel>(dirty):
I/flutter (21159): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 481 pos 15: 'value == null ||
I/flutter (21159): items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
我的代码:
Widget _buildDropDownList(MainModel model) {
isEmpty: itemId == 0,
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
value: itemId != 0 ? itemId .toString() : null,
isDense: true,
onChanged: (String newValue) {
setState(() {
itemId = model.allItems
.where((x) => x.itemId == int.parse(newValue))
.first
.itemId;
});
},
items: model.allItems.map((Item item) {
return new DropdownMenuItem<String>(
value: item.itemId.toString(),
child: new Text(item.itemName),
);
}).toList(),
),
),
);
}
我认为这是由与时间有关的问题引起的,因为当我设置时
value = null
然后正确填充了下拉列表,但是当然没有选择任何元素。