select flutter 下拉按钮的初始值

select initial value for a Dropdown button in flutter

我搜索了与我的问题类似的答案,但我的答案不够,如果困难的话也许我的方法。

这是我想要做的, 我有一个 API 国家值。 为了注册用户,我使用了如下所示的下拉列表,它将 CountryApi 值映射到它的项目。 我手里只有国名

如何设置该下拉菜单的初始值以匹配我的国家/地区名称?

国家/地区 Select DropDown

CountryModal _selectedCountry;

onChangeDropdownItem(CountryModel selectedCountry) {
    setState(() {
      // fieldFocusChange(context, _countryFocusNode, _phoneFocusNode);
      _selectedUserCountry = selectedCountry;
      _userCountry = _selectedUserCountry.name;
      countryCodeTxt = _selectedUserCountry.dial_code;
      countryCode = _selectedUserCountry.code;
      _userCountryId = _selectedUserCountry.id.toString();
    });
  }
/////
userCountryDropdown = Container(
      padding: EdgeInsets.all(2.0),
      width: MediaQuery.of(context).size.width,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(
            10.0,
          )),
          color: Color(0xFFF0F0F0),
          shape: BoxShape.rectangle),
      child: DropdownButton<CountryModel>(
        isExpanded: true,
        hint: Text(
          'Select Country',
          style: kMainContentStyleLightBlack,
        ),
        autofocus: true,
        value: _selectedUserCountry,
        isDense: false,
        onChanged: onChangeDropdownItem,
        items: _countryList.map((country) {
          return DropdownMenuItem<CountryModel>(
            child: new Text(country.name),
            value: country,
          );
        }).toList(),
        style: kMainContentStyleLightBlack,
      ),
    );

我的国家 API 看起来是这种格式

国家/地区API

[
  {
    "id": 1,
    "code": "AF",
    "dial_code": "+93",
    "name": "افغانستان"
  },
  {
    "id": 2,
    "code": "AX",
    "dial_code": "+358",
    "name": "Åland"
  },
....

有人可以帮我解决这个问题并解决这个问题吗?

value 属性 指定下拉列表的默认选定值。
    Container(
  padding: EdgeInsets.all(2.0),
  width: MediaQuery.of(context).size.width,
  decoration: BoxDecoration(
      borderRadius: BorderRadius.all(Radius.circular(
        10.0,
      )),
      color: Color(0xFFF0F0F0),
      shape: BoxShape.rectangle),
  child: DropdownButton(
    isExpanded: true,
    hint: Text(
      'Select Country',
      style: kMainContentStyleLightBlack,
    ),
    autofocus: true,
    value: _selectedUserCountry,
    isDense: false,
    onChanged: onChangeDropdownItem,
    items: _countryList.map((country) {
      return DropdownMenuItem(
        child: new Text(country.name),
        value: country.name,
      );
    }).toList(),
    style: kMainContentStyleLightBlack,
  ),
);

我没有仔细阅读你的问题,但这里是你的参考。 dropdownValue这里是默认值

 Padding(
                    padding: EdgeInsets.all(8.0),
                    child: DropdownButton<String>(
                      value: dropdownValue,
                      icon: Icon(Icons.arrow_drop_down),
                      iconSize: 24,
                      elevation: 16,
                      // style: TextStyle(color: Colors.white),
                      underline: Container(
                        height: 2,
                        width: double.infinity,
                        // color: Colors.deepPurpleAccent,
                      ),
                      onChanged: (String newValue) {
                        setState(() {
                          print(newValue);
                          dropdownValue = newValue;
                        });
                      },
                      items: <String>['Male', 'Female']
                          .map<DropdownMenuItem<String>>((String value) {
                        return DropdownMenuItem<String>(
                          value: value,
                          child: Text(value),
                        );
                      }).toList(),
                    ),
                  ),