Finn
Finn

Reputation: 201

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

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?

Upvotes: 1

Views: 1652

Answers (1)

h8moss
h8moss

Reputation: 5020

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;
}

Upvotes: 5

Related Questions