Home > Blockchain >  flutter In dropdownButton how can I assign the first value of list to be selected by default
flutter In dropdownButton how can I assign the first value of list to be selected by default

Time:03-17

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

enter image description here

                    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(),
                    ),
                  ),

CodePudding user response:

For the first time you have to provide an initial value to _level which match to your list item enquiryController.levelsList. If _level value did not match with any list item of enquiryController.levelsList then it throw the above error.

  • Related