【问题标题】:What is the best practice to display data from API call to a Flutter DropdownButton?显示从 API 调用到 Flutter DropdownButton 的数据的最佳做法是什么?
【发布时间】:2021-05-18 19:29:11
【问题描述】:

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

【问题讨论】:

  • 如果它是 Future,是的,要么是 FutureBuilder,要么使用来自 RiverPod 的 FutureProvider,它具有 AsyncValue.when 的样板代码比 FutureBuilder 少。

标签: flutter dart dropdownbutton


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 2016-12-30
    • 2011-06-30
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多