flutter - 显示从 API 调用到 Flutter DropdownButton 的数据的最佳实践是什么?
问题描述
目前我的应用程序正在向一个 API 发出一个 http 请求,该请求为我提供了 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(),
),
),
)
推荐阅读
- airflow - 使用 KubernetesExecutor 将 ConfigMap 挂载到 Airflow Worker Pod
- kubernetes - Rancher K8s 头盔
- javascript - 在 ReactJS 中完成验证后如何重定向页面
- python - 生成所有三个数字的集合,总和为 12 python
- javascript - 如何解决“在 PATH 错误中找不到运行时‘节点’”
- python - 无法将文件复制到新目录 - TypeError: os.PathLike object
- kotlin - Bucles for and while
- reactjs - 在 React 中编辑特定的表格行内容
- python - 如果您使用“to_json”保存熊猫数据框,并且索引有一个新标签,有没有办法在文件中保留该标签名称?
- python - Python 中的 BaseXClient:检索问题