Home > Net >  How can I check a String in Flutter for only line breaks or spaces? (Flutter, Dart)
How can I check a String in Flutter for only line breaks or spaces? (Flutter, Dart)

Time:12-13

I want to check if a TextInputField has input containing only line breaks or spaces.

In other words, if the input contains anything other than line breaks or spaces.

How can I do that?

CodePudding user response:

I believe the trim method combined with isEmpty will be of use here

void main() {
  print(checkEmpty(''));                // true
  print(checkEmpty('          '));      // true
  print(checkEmpty('\n\n\n\n'));        // true
  print(checkEmpty('             z'));  // false
  print(checkEmpty('\n\t'));            // true
}

bool checkEmpty(String val) {
  return val.trim().isEmpty;
}
  • Related