Api 为颤振中的下拉按钮选择初始值

Api 为颤振中的下拉按钮选择初始值,api,flutter,dropdown,Api,Flutter,Dropdown,对于我的问题,我已经找到了类似的答案,但我的答案还不够,也许我的方法很难 这就是我要做的, 我有一个国家价值观的API。 为了注册用户,我使用下面的下拉列表,将CountryApi值映射到其项。 我手里只有国名 如何设置该下拉列表的初始值以与我拥有的国家名称匹配 国家/地区选择下拉列表 有人能帮我解决这个问题吗?我没怎么读你的问题,但这是给你的参考资料dropdownValue是此处的默认值 Container( padding: EdgeInsets.all(2.0), wid

对于我的问题,我已经找到了类似的答案,但我的答案还不够,也许我的方法很难

这就是我要做的, 我有一个国家价值观的API。 为了注册用户,我使用下面的下拉列表,将CountryApi值映射到其项。 我手里只有国名

如何设置该下拉列表的初始值以与我拥有的国家名称匹配

国家/地区选择下拉列表


有人能帮我解决这个问题吗?

我没怎么读你的问题,但这是给你的参考资料dropdownValue是此处的默认值

    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,
  ),
);
 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(),
                    ),
                  ),
填充(
填充:边缘设置。全部(8.0),
孩子:下拉按钮(
value:dropdownValue,
图标:图标(图标。箭头下拉),
iconSize:24,
海拔:16,
//样式:TextStyle(颜色:Colors.white),
下划线:容器(
身高:2,
宽度:double.infinity,
//颜色:颜色。深紫色,
),
onChanged:(字符串newValue){
设置状态(){
打印(新值);
dropdownValue=newValue;
});
},
物品:[“男性”、“女性”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
),
),

无法将参数类型“dynamic Function()”分配给参数类型“void Function(CountryModel)”。它给出了这个错误。我把这个函数设置为匿名函数。之前的评论是错误的。我再次进行了更改,删除了DropdownButton和DropdownMenuItem中的显式类型干扰,以便您可以再次为country name赋值。您显示的方法有效,尽管我必须先获取匹配对象,然后将country name映射到该对象。将该对象设置为_selectedUserCountry,一切正常。谢谢如果可以的话,请阅读问题。我的初始值是该国家API中的国家名称。我需要将该国家名称设置为下拉列表中的初始值(从国家API加载项目)。
    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,
  ),
);
 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(),
                    ),
                  ),