Search code examples
validationemailfluttervar

How to put a var instead of a string in Email Validation package


I need to put a var in the string place in email validator:

var email;

void validation(){
bool emailvalidated = EmailValidator.validate(email);
if (emailvalidated) {
print('object');
}
}

but it throw me this error : Invalid arguments: email. Help!!


Solution

  • You are getting the error because the validate method needs a string.

    Replace your code with the one below: It works perfectly well:

    // change your email variable to a have a default string
    var email = 'youremail.com';
    
    void validation(){
    bool emailvalidated = EmailValidator.validate(email);
    if (emailvalidated) {
    print('object');
    }
    }
    

    I hope it helps.

    UPDATED

    Add the variables below to your class:

     // define a controller to assign to your custom text field
      TextEditingController emailEditingController = TextEditingController();
    
     // declare the email variable here
      String email;
    

    Replace your emailTextFormField widget with the one below:

     Widget emailTextFormField() {
        return CustomTextField(
          // assign your email controller to the custom text field here
          textEditingController: emailEditingController,
          keyboardType: TextInputType.emailAddress,
          icon: Icons.email,
          hint: "Email ID",
          textEditingController: email,
        );
      }
    

    Replace this with your validation function

     void validation() {
    // email should be instantiated here
      email = emailEditingController.text;
        bool emailvalidated = EmailValidator.validate(email);
        if (emailvalidated) {
          print('Email validated');
        } else{
          print('email not validated');
         }
      }
    

    I hope this helps.