Home > Software design >  The argument type 'Pattern' can't be assigned to the parameter type 'String'
The argument type 'Pattern' can't be assigned to the parameter type 'String'

Time:10-13

I'm just starting out with authentication and have this error.

validator: (value) {
                          Pattern pattern =
                              r'^(([^<>()[\]\\.,;:\s@\"] (\.[^<>()[\]\\.,;:\s@\"] )*)| (\". \"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$';
                          RegExp regex = new RegExp(pattern);
                          if (!regex.hasMatch(value!))
                            return 'Enter a valid email';
                          else
                            return null;
                        },

Copied from here: https://medium.com/swlh/how-to-implement-autofill-in-your-flutter-app-b43bddab1a97

But fixed one error, had to add null check to (value!)). Is this a similar problem?

CodePudding user response:

Here is the reference, Check the below code

validator: (value) {
if (value != null || value.isNotEmpty) {
final RegExp regex =
  RegExp(r'^(([^<>()[\]\\.,;:\s@\"] (\.[^<>()[\]\\.,;:\s@\"] )*)| (\". \"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$');
  if (!regex.hasMatch(value!))
      return 'Enter a valid email';
  else
      return null;
} else {
  return 'Enter a valid email';
}},
  • Related