如何在Flutter中验证动态创建的文本字段

时间:2020-05-13 20:25:09

标签: validation flutter textfield

布尔变量的值发生变化,但在文本字段中未显示错误文本

这些是声明

    int i = 0;
    List<bool> validationList = [];
    List<Widget> listDynamic = [];
    List<TextEditingController> controllersList = []; 

这是验证方法

    submitData()
    {
    for(int a = 0 ; a < i; a++ ){
        if(controllersList[a].text == ''){
            setState(() {
                validationList[a] =true;
                print(validationList[a]);
            });
        }
    }
    }

这是动态创建文本文件的方法

    addDynamic() {
    validationList = List.from(validationList)..add(false);
    controllersList = List.from(controllersList)..add(TextEditingController());
    listDynamic = List.from(listDynamic)..add(Column(
        children: <Widget>[
            Container(
            margin: EdgeInsets.fromLTRB(20, 40, 20, 0),
            child: TextField(
                decoration: InputDecoration(
                    errorText: validationList[i] == true ? 'Value cant be empty' : null,
                    labelText: ('ImageURL'),
                    prefixIcon: Icon(Icons.image),
                    helperText: 'www.example.com',
                    border: OutlineInputBorder()),
                    controller: controllersList[i],
            ),
          ),
          SizedBox(height: 20),
        ],
      ));
      i++;
    setState(() {});
    }

1 个答案:

答案 0 :(得分:0)

将'key'属性添加到TextField中: 键:ObjectKey(“ key $ i”),