onChanged: (v) async {
print(EmailValidator.validate(v));
EmailValidator.validate(v)
? () async {
print('email tureeee');
}
: () async {
print('email not trueee');
};
},
I have this conditional functions inside the onChange()
of the TextField widget. print(EmailValidator.validate(v));
seems to work. However, the conditional function does not execute when condition has been met.
How can I make this work?
CodePudding user response:
Actually the issue is here, you are creating another function. Since the onChanged
calls when ever text changes. Now based on validation you are creating another anonymous function, which is need to be call to execute.
You can simply check the execution like
TextFormField(onChanged: (value) {
value.isEmpty
? () async {
print('email tureeee');
}
: () async {
print('email not trueee');
};
})
Now let say you like use this format executes the function. For this you can create and execute function like (){...}()
TextFormField(onChanged: (value) {
value.isEmpty
? () async {
print('email tureeee');
}()
: () async {
print('email not trueee');
}();
})
The same things go for EmailValidator.validate(v)
. But instead of nesting like this, it would be better creating single bool
and handle check operate to have clean code.
CodePudding user response:
try this:
onChanged: (v) async {
print(EmailValidator.validate(v));
if(EmailValidator.validate(v)){
print('email tureeee');
}else{
print('email not trueee');
}
},
CodePudding user response:
you're basically creating an anonymous function, which is never being called, and it's quite unnecessary here
you can simply have if-else like shown below
plus async
can be avoided, if you're not using await
keyword inside onChanged
onChanged: (v) {
final result = EmailValidator.validate(v);
print(result);
if (result) {
print('email tureeee');
} else {
print('email not trueee');
}
},