首页 > 解决方案 > 从 DropdownItems 中选择值后,DropdownButton 值未更新。如何使用 selectedLocation 更新默认值?

问题描述

从 DropdownItems 中选择值后,DropdownButton 值未更新。如何使用 _selectedLocation 更新默认值?
我使用的变量:

  String? stateToCountry;
  List? stateID;
  List? cityJson;
  Position? _position;
  String? currentAddress;
  Placemark? place;
    DropdownButton(
               hint:_selectedLocation==null ? Text('Dropdown') : Text(
                 _selectedLocation.toString(),
                 style: TextStyle(color: Colors.black),
               ),
              isExpanded: true,
              iconSize: 30,
              elevation: 16,
              underline: Container(
                height: 2,
                color: Colors.deepPurpleAccent,
              ),
              style: TextStyle(color: Colors.deepPurple, fontSize: 20.0),
              itemHeight: 50,
              items: countrylist!.map((location){
                return DropdownMenuItem(
                  value: location,
                  child: new Text(location["name"].toString()),

                );
              }).toList(),
              onChanged: (newValue){
                 setState(() {
                   _selectedLocation=newValue as String?;
                 });
              },

                ),

标签: flutterdart

解决方案


您需要设置 value 属性,

DropdownButton(
  value: _selectedLocation,
  onChanged:(newValue){
      setState(() {
        _selectedLocation=newValue;
      });
  }

推荐阅读