简体   繁体   English

flutter 在 dropdownButton 如何分配列表的第一个值默认选择

[英]flutter In dropdownButton how can I assign the first value of list to be selected by default

I'm new to flutter, I have dropdown list it works fine and I able to get data from Api, but instead of put hint text, I want the first value of the list that fetch from api to be selected, here the value of dropdown is a String but if I put any string I get error so I want to check if _level is null then read from enquiryController.levelsList.first.name but I'm getting this error我是 flutter 的新手,我有下拉列表,它工作正常,我能够从 Api 获取数据,但我不想放置提示文本,而是希望选择从 api 获取的列表的第一个值,这里的值下拉列表是一个字符串,但如果我输入任何字符串,我会收到错误信息,所以我想检查 _level 是否为 null,然后从 enquiryController.levelsList.first.name 中读取,但出现此错误

在此处输入图像描述

                    child: DropdownButton<String>(
                      autofocus: true,
                      isDense: false,
                      value: _level,
                      items:  enquiryController.levelsList.map((item) {
                        return new DropdownMenuItem(
                          child: new Text(item.name),
                          value: item.id,
                        );
                      }).toList(),
                      onChanged: (value) {
                        _level = value;
                    enquiryController.setLevelIndex(value, true);
                      },
                      isExpanded: true,
                      underline: SizedBox(),
                    ),
                  ),

For the first time you have to provide an initial value to _level which match to your list item enquiryController.levelsList .第一次你必须为_level提供一个初始值,它与你的列表项enquiryController.levelsList相匹配。 If _level value did not match with any list item of enquiryController.levelsList then it throw the above error.如果_level值与enquiryController.levelsList的任何列表项都不匹配,则会抛出上述错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在flutter中更改dropdownButton的默认值? - How to change default value of dropdownButton in flutter? Flutter 下拉按钮选择的值没有改变 - Flutter Dropdownbutton selected value is not changing 在 DropdownButton 中绑定所选值的列表 - bind list of selected value in DropdownButton Flutter 中的下拉按钮不会将值更改为所选值 - DropdownButton in Flutter not changing values to the selected value 如何根据 Flutter 中第一个 DropdownButton 的选择加载第二个 DropdownButton? - How do I load a second DropdownButton based on selection from first DropdownButton in Flutter? 我想在 flutter 中创建一个可重复使用的自定义 DropdownButton,这里提示名称和列表对于所有 DropdownButton 都是不同的 - I want to create a custom DropdownButton in flutter which can be reusable, here the hint name and list will be different for all DropdownButton 如何在 DropdownButton flutter 中的数组中放置 String 的 snapShot 列表 - How can I put a list of snapShot of String in array inside the DropdownButton flutter 如何使用 JSON 数据列表创建 DropdownButton,我希望它在 Flutter 中填充我的 DropDownButton - How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter 如何在 flutter 中获取下拉按钮的选定索引 - how to get selected index of dropdownbutton in flutter Flutter - 如何对齐从 DropdownButton 中选择的项目? - Flutter - How to align selected item from DropdownButton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM