Issue
I am trying to setup password and email validation and I am getting the error above. Any help would be greatly appreciated. The error above is in the main.dart code and has been bolded in the code.
validator.dart code
enum FormType { login, register }
class EmailValidator {
static String? validate(String value) {
return value.isEmpty ? "Email can't be empty" : null;
}
}
class PasswordValidator {
static String? validate(String value) {
return value.isEmpty ? "Password can't be empty" : null;
}
}
main.dart code
List<Widget>buildInputs() {
return [
TextFormField(
validator: **EmailValidator.validate**,
decoration: InputDecoration(labelText: 'Email'),
onSaved: (value) => _email = value,
),
TextFormField(
validator: **PasswordValidator.validate**,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
onSaved: (value) => _password = value,
),
];
}
Solution
If you check validator
it returns a nullable String.
{String? Function(String?)? validator}
You can convert your validators like
class EmailValidator {
static String? validate(String? value) {
return value==null || value.isEmpty ? "Email can't be empty" : null;
}
}
class PasswordValidator {
static String? validate(String? value) {
return value==null ||value.isEmpty ? "Password can't be empty" : null;
}
}
Answered By - Yeasin Sheikh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.