首页 > 解决方案 > 显示从 API 调用到 Flutter DropdownButton 的数据的最佳实践是什么?

问题描述

目前我的应用程序正在向一个 API 发出一个 http 请求,该请求为我提供了 json 并列出了一些项目,这个 json 被分配给一个 List 变量。我已经将这些数据拉入我的应用程序,但需要在 DropDownButton 上显示它。要在下拉按钮中显示此数据,我应该使用 FutureBuilder 还是有类似的最佳实践?

标签: flutterdartdropdownbutton

解决方案


你可以尝试这样的事情, 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(),
                                              ),
                                            ),
                                          )

推荐阅读