【问题标题】:Flutter Dropdown List Issue颤振下拉列表问题
【发布时间】:2022-01-01 22:06:50
【问题描述】:

我有一个 phpmysql 数据库。我正在从数据库中获取数据。我想将我的类别数据显示到我的下拉按钮中。但它只显示第一个数据。这是我的代码。

SizedBox(
                                width: context.dynamicWidth(5),
                                height: context.dynamicHeight(5),
                                child: ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: snap.length,
                                  itemBuilder: (context, index) {
                                    var categoryList =
                                        snap[index]['categoryName'];
                                    return DropdownButton<String>(
                                      items: <String>[
                                        categoryList,
                                      ].map((String value) {
                                        return DropdownMenuItem<String>(
                                          value: value,
                                          child: Text(value),
                                        );
                                      }).toList(),
                                      onChanged: (_) {},
                                    );
                                  },
                                ),
                              ),

那只显示第一个数据。谢谢帮忙。

【问题讨论】:

  • 试试我的回答here希望对你有帮助。

标签: flutter


【解决方案1】:

我不知道你的快照列表类型,但希望这项工作是

SizedBox(
                                width: context.dynamicWidth(5),
                                height: context.dynamicHeight(5),
                                child: ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: snap.length,
                                  itemBuilder: (context, index) {
                                    return DropdownButton<String>(
                                      items: snap.map((value) {
                                        return DropdownMenuItem<String>(
                                          value: value['categoryName'],
                                          child:Text(value['categoryName']),
                                        );
                                      }).toList(),
                                      onChanged: (_) {},
                                    );
                                  },
                                ),
                              ),

【讨论】:

  • 那行得通。谢谢。你是完美的。
【解决方案2】:

试试

    var categoryList = snap[index];

而不是

    var categoryList = snap[index]['categoryName'];

【讨论】:

  • 抱歉没有工作。这表明ibb.co/whQDQHP
  • return DropdownMenuItem&lt;String&gt;( value: value['categoryName'], child: Text(value), );
  • 对不起还是一样:(
猜你喜欢
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2021-11-25
  • 1970-01-01
  • 2019-05-25
  • 2020-09-30
相关资源
最近更新 更多