应该只有一个项目具有Dropdown的Value Error

时间:2020-11-03 10:09:43

标签: flutter flutter-layout flutter-form-builder

我完全坚持解决此错误,对这个平台来说是新手,任何人都可以帮助我清除此问题

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)),
          )
        ]),
      ),
     
    );
  }
}

enter image description here

我已附上该错误的屏幕截图,希望它能告诉您问题所在

1 个答案:

答案 0 :(得分:1)

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

您已经用空字符串初始化了 _location_id ,这是DropDownButton在列表项中查找不存在的空字符串ID的原因,该错误已引发。

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