0

我完全无法解决这个错误,是这个平台的新手,任何人都可以帮我清除这个

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

在此处输入图像描述

我附上了错误的屏幕截图,希望它能说明问题所在

4

1 回答 1

1

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

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

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

于 2020-11-03T11:12:12.823 回答