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.
pubspec.yaml

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: https://github.com/vishnuvichu14/flutter_email_validator

Thank you for reading this article.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Firebase Database Pagination — Android 🔥

How to turn your Android into Landscape mode on FlipSetter’s whiteboard

14.2 Playing Nicely with the Rest of the System

Continues Delivery Pipelines using Jenkins for Android

Version Management in Flutter with FVM

Flutter: How to make WebView transparent

A simple way of creating a custom ROOM database backup in Android

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
Vishnuvarshan P

Vishnuvarshan P

More from Medium

Building Android Widgets In Flutter: Remember these Simple Points

Why Flutter Is The Best Framework For Hybrid App Development

Flutter Interview Questions

Flutter AndroidView forbidden parameter