无法在Flutter中进行列表编辑将值设置为下拉按钮

时间:2019-03-12 07:08:32

标签: flutter flutter-layout dropdownbutton

我正在尝试从API填充一个下拉列表,该列表工作正常,已在表单中使用,并且所有表单提交均显示在列表中。 在我进行编辑时,从列表中,所有字段都填充了列表项中的值,而不是下拉按钮,

这是下拉按钮的代码 我从列表中获得类别的价值,但得到以下错误

  

断言失败:560行pos 15:'item == null || items.isEmpty ||   值== null || items.where(((DropdownMenuItem item)=> item.value   == value).length == 1':不正确。

感谢您的帮助。

下面附上我用于列表和编辑的代码

list Widget _buildWidget() {
    return new RefreshIndicator(
        child: new ListView.builder(
          padding: new EdgeInsets.all(10.0),
          itemCount: data == null ? 0 : data.length,
          itemBuilder: (BuildContext context, i) {
            return new Column(
              children: <Widget>[
                new Padding(padding: EdgeInsets.only(top: 20.0)),
                new ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                  child: new Container(
                    padding: new EdgeInsets.all(10.0),
                    color: Colors.white70,
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        new Text((i+1).toString(), style: new TextStyle(fontSize: 14.0),),
                        new Text(data[i]['Category_name'],style: new TextStyle(fontSize: 14.0, color: Colors.black),),
                        new Text(data[i]['name'],style: new TextStyle(fontSize: 14.0, color: Colors.black),),
                        new Container(
                          child: new Row(
                            children: <Widget>[
                              new IconButton(icon: new Icon(Icons.edit), onPressed: (){
                                debugPrint("Edit");
                                Navigator.pushReplacement(context,
                                    MaterialPageRoute(builder: (context) => new ItemEdit(list: data, index:i)));
                              }),
                              new Switch(
                                  value: (data[i]['status']=="0" ? true : false),
                                  onChanged: (bool value) {
                                    debugPrint(value.toString());
                                    String status = (value.toString()=="false")?"1":"0";
                                    debugPrint("Status :: "+status);
                                    changeItemStatus(data[i]['itemid'], status);
                                    setState(() {
                                       value = (data[i]['status']=="0" ? false : true);
                                      _saving = false;
                                    });
                                  }
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            );
          },
        ),
        onRefresh: getItems);
  }

编辑

Widget _buildWidget() {
    return Form(
      key: _formKey,
      child: Container(
        margin: new EdgeInsets.all(20.0),
        child: new Column(
          children: <Widget>[
            new TextFormField(
              controller: _itemName,
              keyboardType: TextInputType.text,
              autocorrect: false,
              maxLength: 20,
              autofocus: true,
              style: new TextStyle(
                  fontFamily: 'Quicksand', color: Colors.black, fontSize: 16.0),
              decoration: InputDecoration(
                icon: new Icon(
                  Icons.category,
                  color: Colors.black,
                ),
                labelText: 'Item Name',
                counterText: "",
                labelStyle: TextStyle(color: Colors.black),
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.black)),
              ),
              validator: (val) {
                if (val.isEmpty) {
                  return "Name can't be empty";
                }
                if (val.isNotEmpty && val.length < 3) {
                  return "Name is too short";
                }
              },
            ),
            new TextFormField(
              controller: _description,
              keyboardType: TextInputType.multiline,
              maxLines: 3,
              autocorrect: false,
              maxLength: 50,
              autofocus: true,
              style: new TextStyle(
                  fontFamily: 'Quicksand', color: Colors.black, fontSize: 16.0),
              decoration: InputDecoration(
                icon: new Icon(
                  Icons.edit,
                  color: Colors.black,
                ),
                labelText: 'Description',
                counterText: "",
                labelStyle: TextStyle(color: Colors.black),
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.black)),
              ),
              validator: (val) {
                if (val.isEmpty) {
                  return "Description can't be empty";
                }
                if (val.isNotEmpty && val.length < 5)
                  return "Description too short";
              },
            ),
            new Row(
              children: <Widget>[
                new Icon(Icons.category),
                new Padding(padding: EdgeInsets.only(left: 20.0)),
//                new Text(category),
                new Flexible(
                  child: new Container(
                    child: new DropdownButton<String>(
                      value: category,
                      hint: new Text("Select Category"),
                      onChanged: (String newValue) {
                        setState(() {
                          category = newValue;
                        });
                      },
                      isExpanded: true,
                      items: catJson.map((Map map) {
                        return new DropdownMenuItem<String>(
                          value: map["id"].toString(),
                          child: new Text(
                            map["name"],
                          ),
                        );
                      }).toList(),
                    ),
                  ),
                ),
              ],
            ),
            new MaterialButton(
              height: 35.0,
              minWidth: 100.0,
              color: Colors.blueAccent,
              textColor: Colors.white,
              child: new Text(
                "Create",
                style: new TextStyle(
                  fontSize: 18.0,
                  fontFamily: 'Quicksand',
                ),
              ),
              onPressed: () => _formKey.currentState.validate()
                  ? updateItem()
                  : Toast.show('Validation failed', context,
                      gravity: Toast.BOTTOM),
              splashColor: Colors.redAccent,
            )
          ],
        ),
      ),
    );
  }

0 个答案:

没有答案