Dart/Flutter: Check if String is Empty, Null, or Blank example

The String data type is used to store sequences of characters in Dart and Flutter, enclosed in either double ("") or single (”) quotes.

By default, if a string variable is declared but not assigned a value, its default value is null.

Understanding the difference between null, empty, and blank strings is crucial:

  • A null string is a variable declared but not assigned a value, resulting in null.
  • An empty string is assigned with "", having a length of zero.
  • A blank string contains at least whitespace characters, with a length greater than zero.

This tutorial provides various methods to check for empty, null, or blank strings in Dart and Flutter.

Check String is empty, null, in Dart and Flutter

In Dart, there are various methods available to determine whether a given string is empty, null, or blank.

use the isEmpty and isNotEmpty method

Dart strings provide built-in methods for checking their status:

  • isEmpty: Returns true if the string is empty or null; false otherwise. Note that it also returns false for strings containing only blank characters.
  • isNotEmpty: Returns true if the string is not empty and not null; false otherwise.

Here is an example program

void main() async {
  String? str = "";

  print(str);
  print(str.isEmpty); //true
  print(str.isNotEmpty); //false

  String str1 = " ";
  print(str1.isEmpty); //false
  print(str1.isNotEmpty); //true

  String str2 = "string";
  print(str2.isEmpty); //false
  print(str2.isNotEmpty); //true
}

Disadvantages with approach

  • isEmpty returns false for blank characters such as white space.
  • isNonEmpty returns true for blank spaces and is not able to differentiate valid string and blank spaces.
  • Another calling this method on a nullable type operator (Question mark after ) for a variable throws the below error
  String? str = null; // or just declared without assigning value

 // throws an error
  print(str.isEmpty);
  print(str.isNotEmpty);

And the error is Error: Property ‘isEmpty’ cannot be accessed on ‘String?’ because it is potentially null. print(str.isEmpty); //false ^^^^^^^ lib/main.dart:7:13: Error: Property ‘isNotEmpty’ cannot be accessed on ‘String?’ because it is potentially null. print(str.isNotEmpty); //true

However, these methods do not work with nullable type operators (?), and attempting to do so will result in an error. Another way to check using a string.length

use string length

Another approach is to utilize the length property of the String class, which returns the length of the string.

Syntax:

int get length

Returns integer numbers from zero to any number.

void main() async {
  String str = "";
  print(str.length); //0
  String str1 = " ";
  print(str1.length); //1
  String str2 = "string";
  print(str2.length); //6
}

How to check if String contains blank characters in dart and flutter

To determine if a string contains only blank characters, you can use the trim() method to remove leading and trailing whitespace, then check if the resulting string is empty.

void main() async {

  String str1 = " ";
  print(str1.trim().isEmpty); //true

}

Conclusion

In conclusion, we’ve explored methods to check for empty, null, or blank strings in Dart and Flutter, enhancing our ability to handle string data effectively.