使用Flutter,我有一个包含AutoCompleteTextField的表单,我试图更改工作中的文本字段的值。使用链接到我之前的问题的技术打开表单时,要编辑的值将放入AutoCompleteTextField中:
How to set the initial value of Flutter autocomplete_textfield
现在,我发现如果尝试更改该字段的值,自动完成功能似乎可以工作,但是在提交表单时,新文本不会出现在表单数据中,因此保留了先前的条目。我尝试将新文本添加到表单数据中,但这不会改变行为。
“ itemSubmitted”代码如下:
itemSubmitted: (species) {
setState(() {
searchTextField.textField.controller.text = species.commonName;
});
submit方法调用validate并将其保存在表单数据上
if (_formKey.currentState.validate() == false) {
return;
}
_formKey.currentState.save();
我还需要做些什么才能在公司数据中更新AutoCOmpleteTextField中的新选择?
更新: 看来,如果我以空白表格开头,则可以在AutoCompleteTextField中搜索条目,将数据保存到数据库中(无需清除表格),搜索新值,然后将数据正确放置到表格中每次数据。这表明打开表单时字段的初始化导致了问题。
Sid
答案 0 :(得分:0)
This issue was entirely of my own making, I am using the same code to both create and edit an entry. To differentiate between new and edit I was testing for valid object being passed to the page, my mistake was that when my text search field was updated I was simply checking if there was a valid object had been passed to the page, of course, the new data was in the form data structure and was never being placed into the object being edited.
The solution was to simply first check if there is a valid input object, if there is then a secondary test is made to see if the form data matched the input object, if not the new data is then placed into the input object.