如何使用下拉列表中的将来构建器获取下拉列表的值

问题描述 投票:0回答:1

我正在使用将来的构建器从将来的响应中获取数据,并将数据加载到下拉列表中。.将来的构建器的零件始终是成功的,但是下拉菜单将无法正确显示结果,这只会简单地崩溃,这可能是从HTTP请求的下拉列表中加载日期的最佳方法

这里获取请求


Future<List<Category>> getCategories(http.Client client) async {
  final response = await client.get(globals.apiUrl + '/categories/',
      headers: {HttpHeaders.authorizationHeader: "Bearer " + globals.accessToken});

  if (response.statusCode == 200) {
    return compute(parseCat, response.body);
  } else {
    throw Exception('Failed to volunteer in this event');
  }
}

下拉列表代码

FutureBuilder<List<Category>>(
future: getCategories(http.Client()),
builder: (BuildContext context,
AsyncSnapshot<List<Category>> snapshot) {

if (snapshot.hasData) {
if (this.categoriesList.length > 0) {
return
DropdownButton<int>(

value: this.categoriesList[0].id,),
onChanged: (int newValue) {
setState(() {
selectedCategory = newValue;

});
},
items: this
.categoriesList
.map<DropdownMenuItem<int>>(
(Category catg) {
return DropdownMenuItem<int>(
value: catg.id,
 child: Text(catg.nameEn),
);
}).toList(),
);
}}}})),

flutter dart drop-down-menu httpresponse
1个回答
0
投票

[当您拨打将来的电话时,它将返回您需要的数据。在FutureBuilder中,如果snapshot.data,将在snapshot.hasData中存储数据;如果snapshot.hasError,则将显示错误。

© www.soinside.com 2019 - 2024. All rights reserved.