Dart String isEmpty
Syntax & Examples


Syntax of String.isEmpty

The syntax of String.isEmpty property is:

 bool isEmpty 

This isEmpty property of String checks whether this string is empty.

Return Type

String.isEmpty returns value of type bool.



✐ Examples

1 Check if an empty string is empty

In this example,

  1. We create an empty string str1.
  2. We then access the isEmpty property of str1 to check if it is empty.
  3. Since str1 is empty, the isEmpty property returns true.
  4. We print the result to standard output.

Dart Program

void main() {
  String str1 = ''; // empty string
  bool isEmpty1 = str1.isEmpty;
  print('Is str1 empty: $isEmpty1');
}

Output

Is str1 empty: true

2 Check if a non-empty string is empty

In this example,

  1. We create a non-empty string str2 with the value 'ABC'.
  2. We then access the isEmpty property of str2 to check if it is empty.
  3. Since str2 is not empty, the isEmpty property returns false.
  4. We print the result to standard output.

Dart Program

void main() {
  String str2 = 'ABC'; // non-empty string
  bool isEmpty2 = str2.isEmpty;
  print('Is str2 empty: $isEmpty2');
}

Output

Is str2 empty: false

3 Check if an empty string is empty

In this example,

  1. We create an empty string str3.
  2. We then access the isEmpty property of str3 to check if it is empty.
  3. Since str3 is empty, the isEmpty property returns true.
  4. We print the result to standard output.

Dart Program

void main() {
  String str3 = ''; // empty string
  bool isEmpty3 = str3.isEmpty;
  print('Is str3 empty: $isEmpty3');
}

Output

Is str3 empty: true

Summary

In this Dart tutorial, we learned about isEmpty property of String: the syntax and few working examples with output and detailed explanation for each example.