Flutter 当在颤振表单中启用autovalidateMode时,当按下submit按钮时,验证程序运行两次
我在Flatter中创建了一个启用autovalidateMode的表单。我的应用程序有两个文本字段来输入姓名和年龄。提交这些值时,它们将插入配置单元数据库。启动应用程序时,它将以列表形式加载数据库值。在验证表单时,我已将autovalidateMode设置为onUserInteraction,并在提交时,应用程序将搜索数据库,查看它是否已经有了插入的名称。如果数据库已经有了名称,那么它将给出一个错误,显示“已经有名称”。我按下的提交按钮有_formKey.currentState.validate()。当我单击提交按钮时,验证程序只能运行一次,但运行两次。因为如果我给出的名称已经在数据库中,错误消息不会显示。如果我禁用autovalidateMode,那么它将正常工作。为什么 功能和属性:-Flutter 当在颤振表单中启用autovalidateMode时,当按下submit按钮时,验证程序运行两次,flutter,android-studio,dart,flutter-form-builder,Flutter,Android Studio,Dart,Flutter Form Builder,我在Flatter中创建了一个启用autovalidateMode的表单。我的应用程序有两个文本字段来输入姓名和年龄。提交这些值时,它们将插入配置单元数据库。启动应用程序时,它将以列表形式加载数据库值。在验证表单时,我已将autovalidateMode设置为onUserInteraction,并在提交时,应用程序将搜索数据库,查看它是否已经有了插入的名称。如果数据库已经有了名称,那么它将给出一个错误,显示“已经有名称”。我按下的提交按钮有_formKey.currentState.valida
final _formKey = GlobalKey<FormState>();
final fieldText_name = TextEditingController();
final fieldText_age = TextEditingController();
bool search = false;
String _name;
String _age;
//clear the text after submit
void clearText() {
fieldText_name.clear();
fieldText_age.clear();
}
//add name and age to database
addContact(Contact contact) async {
await Hive.box('contact').add(contact);
}
//search the name with database to see if it already has that name
searchContact(name) async {
print("searchContact stated :$search");
var length = Hive.box('contact').length;
for (int num = 0; num < length; num++) {
if (name == await Hive.box('contact').getAt(num).name) {
search = true;
break;
}
}
}
表格内:-
TextFormField(
controller: fieldText_name,
validator: (value1) {
RegExp exp1 = RegExp(
r"""[0-9\^\`\~\!\@\#\$\%\&\*\(\)\_\-\+\=\{\}\[\]\|\\\:\;\“"’'\<\,\>\.\?\๐\฿\/]""");
if (value1 == null || value1.isEmpty) {
return 'Empty';
} else if (exp1.hasMatch(value1)) {
return 'Letters only';
} else {
print("validation :$search");
if (search) {
search = false;
print("validation inside if :$search");
return 'Already has the name';
}
}
return null;
},
TextFormField(
控制器:fieldText_名称,
验证人:(value1){
RegExp exp1=RegExp(
r“[0-9\^\`\~\!\@\\\\\$\%\&\*\(\)\\\\-\+\=\\\\\\\\[\]\\\\\\\:\\\\”\๐\฿\/]""");
if(value1==null | | value1.isEmpty){
返回“空”;
}else if(exp1.hasMatch(value1)){
返回“仅限信件”;
}否则{
打印(“验证:$search”);
如果(搜索){
搜索=假;
打印(“如果:$search,则内部验证”);
return“已经有名称”;
}
}
返回null;
},
TextFormField(
controller: fieldText_name,
validator: (value1) {
RegExp exp1 = RegExp(
r"""[0-9\^\`\~\!\@\#\$\%\&\*\(\)\_\-\+\=\{\}\[\]\|\\\:\;\“"’'\<\,\>\.\?\๐\฿\/]""");
if (value1 == null || value1.isEmpty) {
return 'Empty';
} else if (exp1.hasMatch(value1)) {
return 'Letters only';
} else {
print("validation :$search");
if (search) {
search = false;
print("validation inside if :$search");
return 'Already has the name';
}
}
return null;
},