[英]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.