How to validate email in Flutter

Nowadays, app development companies use email for marketing because it’s easily shared. With emails, one can reach thousands of potential app users across the globe in seconds. So Validating emails are very useful. Email validation has become very simple using the email_validator plugin.

  1. Adding email_validator in pubspec.yaml file
    Add the email_validator plugin in the dependencies in pubspec.yaml file. Then click on the pub get on the top.

2. Import the email_validator package to the main.dart file.

Example code

3. In the above example, there is a constant variable named “email” which has the value of a test email.

4. And another variable named “isValid” which is of Boolean data type holds the value EmailValidator.validate(email).

5. Where the email variable is passed in the Emailvalidator.validate() function.

6. The Emailvalidator.validate(email) will return a bool value, if the email is correct or valid it will return true, or else it will return false. The result is stored in the isValid variable, which we can use for our validation.

7. In our example, we have used a simple conditional statement using the ternary operator, if the email is valid, it will print “yes” and if the email is not valid it will print “Not” in the terminal.

Note: Ternary Operator is similar to if-else conditional statement

8. By using this, we can easily identify the valid email and implement it in our apps for email validation. That’s it, it was a very easy and simple package to work with.

If you need the git repository for this simple code, you can get it here:

Thank you for reading this article.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store