flutterflutter-layoutflutter-form-builder

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


I am totally stuck on resolving this error, am new to this platform anyone help me clear this

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

I have attached a screenshot of the error, hope it will tell what's wrong


Solution

  • Just remove the initialisation of _location_id from your initState() method.

    You have initialised the _location_id with empty string that's the reason DropDownButton is looking for empty string id in your list items which is not present and the error has been thrown.

    Solution Just remove the initialisation of _location_id from your initState() method.