2

Is there a shorter way to check if a string is either null or empty? Do I need to write every time 2 conditions inside if to check it?

validator: (value) {
    if (value == null || value.isEmpty){
        //....
    }
}

In python we can do if value and it works. In javascript we could do if(!value).

3 Answers3

1

As from my understanding, the TextEditingController use TextEditingValue.empty which is providing empty string initially. I am not able to get null exception.

I think we can use ! while we are sure the value is empty, not null.

validator: (value) {
  if (value!.isEmpty) return "Empty";

I always prefer checking null first, for nullable data type.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

try this

validator: (value) {
if (value.isNotEmpty){
     print(value);
 }else{
   print("Empty");
 }
}
0

do a guard check

validator: (String? value) {
  if (value?.isNotEmpty == true) {
    return null;
  }
  
  return 'Value cannot be null or empty';
},

this will do, because the combination of ?. and == true will check for null.

while the .isNotEmpty checks as the method's usage, if not empty.

ybbond
  • 51
  • 1
  • 4