Email static method

String? Function(String? value)? Email({
  1. String errorMessage = 'Invalid email',
  2. String? next(
    1. String value
    )?,
})

Returns a validator that checks if a string is a valid email.

Arguments :

  • errorMessage : The error message to return if the string is not a valid email.
  • next : A validator to run after this validator.

Usage :

TextFormField(
validator: Validators.Email(),
),

Usage without TextFormField :

final validator = Validators.Email();

validator('hello'); // 'Invalid email'

You can also chain validators like this:

final validator = Validators.Required(
next: Validators.Email(),
);

Implementation

static String? Function(String? value)? Email({
  String errorMessage = 'Invalid email',
  String? Function(String value)? next,
}) {
  return (value) {
    if (value == null || value.trim().isEmpty) {
      return null;
    }

    if (!MasterValidatorHelpers.checkEmail(value)) {
      return errorMessage;
    }

    if (next != null) {
      return next(value);
    }

    return null;
  };
}