El *_*bre 5 dart dropdown flutter
我正在尝试将一个元素发送到一个视图。此元素将是第二页下拉列表中的默认元素。我在第一页使用类似的东西
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new SummaryPage(,
dropdownItem: dropdownSelection)));
}
Run Code Online (Sandbox Code Playgroud)
然后在第二页我创建未来以获取下拉列表中的所有元素和 initstate 方法来设置默认元素下拉列表
List data = List();
Future<Null> getEstates() async {
final response = await http
.get('URL'
}).catchError((error) {
print(error.toString());
});
final responseJson = json.decode(response.body);
final getBody = responseJson['body'];
setState(() {
data = getBody;
loading = false;
});
}
void initState() {
super.initState();
setState(() {
dropdownSelection = widget.dropdownItem.toString();
});
Run Code Online (Sandbox Code Playgroud)
getEstate 返回这个
[{id: 1, 描述: Terreno Rio}, {id: 2, 描述: Terreno Asier}]
下拉看起来类似于这个
child: ButtonTheme(
alignedDropdown: true,
child: new DropdownButton<String>(
isDense: true,
hint: new Text("Search...",
textAlign: TextAlign.center),
value: dropdownSelection,
onChanged: (String newValue) {
setState(() {
dropdownSelection = newValue;
},
items: data.map((item) {
return new DropdownMenuItem<String>(
child: new Text(
item['description'],
),
value: item['id'].toString(),
);
}).toList(),
),
),
Run Code Online (Sandbox Code Playgroud)
显示的错误是
值 == 空 || items.where((DropdownMenuItem item) => item.value == value).length == 1': 不是真的
显然代码很好,但是当我转到第二个视图时会显示大约 1 秒的错误视图,然后显示第二个页面。我认为该值可能加载得太快,并且下拉菜单无法正确设置。知道为什么会发生此错误以及如何修复它们吗?
那是因为第一次你没有任何值(你的列表是空的),所以你可以显示一个CircleProgressIndicator,像这样:
child: data.length > 0 ? ButtonTheme(
alignedDropdown: true,
child: new DropdownButton<String>(
isDense: true,
hint: new Text("Buscar..",
textAlign: TextAlign.center),
value: dropdownSelection,
onChanged: (String newValue) {
setState(() {
dropdownSelection = newValue;
},
items: data.map((item) {
return new DropdownMenuItem<String>(
child: new Text(
'Finca: ' + item['descripcion'],
),
value: item['id'].toString(),
);
}).toList(),
),
),
) : Center(child: CircularProgressIndicator()),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12527 次 |
| 最近记录: |