如何解决将 API 调用中的数据显示到 Flutter DropdownButton 的最佳实践是什么?
目前,我的应用程序正在向 API 发出 http 请求,该 API 为我提供列出了一些项目的 json,这个 json 被分配给一个 List 变量。我已经将这些数据拉入我的应用程序,但需要在 DropDownButton 上显示它。要在下拉按钮中显示这些数据,我应该使用 FutureBuilder 还是有这样的最佳实践?
解决方法
你可以尝试这样的事情,
WalletRepo.getRestaurantBalance()
用他们当前的余额提取了一堆餐馆
FutureBuilder(
future: WalletRepo.getRestaurantBalance(),builder: (_,AsyncSnapshot<GetRestaurantBalance> snapshot){
if(snapshot.hasData && snapshot.data != null){
return StatefulBuilder(builder: (BuildContext context,void Function(void Function()) nSetState) {
return Column(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),borderRadius: BorderRadius.circular(5)
),padding: EdgeInsets.symmetric(horizontal: 10,vertical: 5),child: DropdownButtonHideUnderline(
child: DropdownButton(
onChanged: (RestaurantBalanceModel restaurant) {
if(restaurant.balance > 0.0){
print(chosenRestaurant);
nSetState(() => chosenRestaurant = restaurant);
}else{
Snack.bottom('Error','Restaurant has no balance to withdraw');
}
},value: chosenRestaurant,isExpanded: true,hint: Text(
'Choose a restaurant'
),items: snapshot.data.getAllRestaurantsByOwner.data.map((restaurant) => DropdownMenuItem(
value: restaurant,child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
Text(
restaurant.name
),Text(
'৳ ${restaurant.balance.toStringAsFixed(1)}'
)
],),)).toList(),)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。