Home > Software design >  Error: Property 'isEmpty' cannot be accessed on 'String?' because it is potentia
Error: Property 'isEmpty' cannot be accessed on 'String?' because it is potentia

Time:09-01

I am trying to check value using if but the app crash on that line. I am using flutter.

 if (value == null || value.isEmpty || !value.contains('@')) {
          return 'Please enter a valid email.';
        }

getting that Error: Property 'isEmpty' cannot be accessed on 'String?' because it is potentially null. Try accessing using ?. instead.

CodePudding user response:

Replace

value.isEmpty

With

(value.toString()).isEmpty

CodePudding user response:

You should add null check for both value!.isEmpty and value!.contains('@'). Like this:

if (value == null || value!.isEmpty || !value!.contains('@')) {
   return 'Please enter a valid email.';
}

or as esentis suggessted:

if ((value?.isEmpty ?? true) || !value!.contains('@')) {
   return 'Please enter a valid email.';
}

CodePudding user response:

Adding my comment as answer so you can have better view of it:

 if (value == null || ( value?.isEmpty ?? true ) || (!value?.contains('@') ?? true )) {
          return 'Please enter a valid email.';
 }

CodePudding user response:

Do this instead

if((value != null && value.isEmpty()) || (value == null)){

}

  • Related