应该只有一项出现下拉菜单的值错误

There Should be exactly one item with Dropdown's Value Error

我完全无法解决这个错误,我是这个平台的新手,谁能帮我解决这个问题

class _EditBasicDetailsScreenState extends State<EditBasicDetailsScreen> {
  final _formKey = GlobalKey<FormState>();

  List _locations = List();
  String _location_id;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _location_id = '';
    _locations = [
      {"location_id": "1", "location_name": "pathirikuppam"},
      {"location_id": "2", "location_name": "koothapakam"},
      {"location_id": "3", "location_name": "thiruvandhipuram"}
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Edit Basic Details"),
      ),
      body: Padding(
        padding: EdgeInsets.all(15.0),
        child: ListView(children: <Widget>[
          Container(
            padding: EdgeInsets.all(5.0),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5.0),
              border: Border.all(
                  color: Colors.pink, style: BorderStyle.solid, width: 0.80),
            ),
            alignment: Alignment.center,
            child: DropdownButtonHideUnderline(
                child: DropdownButton(
                    isExpanded: true,
                    hint: Text('Select Shop Location'),
                    items: _locations.map((dropDowmLocationItem) {
                      return DropdownMenuItem(
                          value: dropDowmLocationItem['location_id'].toString(),
                          child: Text(dropDowmLocationItem['location_name']));
                    }).toList(),
                    onChanged: (String newselectedLocation) {
                      setState(() {
                        _location_id = newselectedLocation;
                      });
                    },
                    value: _location_id)),
          )
        ]),
      ),
     
    );
  }
}

我附上了错误的截图,希望它能告诉我错在哪里

只需从您的 initState() 方法中删除 _location_id 的初始化。

您已经使用空字符串初始化了 _location_id,这就是 DropDownButton 在您的列表项中寻找空字符串 ID 的原因,该 ID 不存在并且错误已抛出。

解决方案 只需从 initState() 方法中删除 _location_id 的初始化。