An empty string means the string has no characters at all — it is just ""
. It’s like having a box with nothing inside.
Checking if a string is empty helps in many situations. For example, when asking for user input, you want to know if the user typed anything or left it blank. It also helps to avoid mistakes when your program needs real data to work with.
Using .isEmpty Property
The .isEmpty
property is a simple way to check if a string has no characters at all. It returns true
if the string length is zero and false
otherwise.
void main() {
String name = '';
if (name.isEmpty) {
print('The string is empty!');
}
}
In the example, we create a string called name
that is empty (''
). Then, we use .isEmpty
to see if name
has no characters. Since it is empty, the program prints a message saying “The string is empty!”. This helps you quickly know when a string has nothing inside.
Using .isNotEmpty
Property
The .isNotEmpty
property tells if a string has one or more characters inside. It returns true
when the string is not empty, and false
if it has no characters.
void main() {
String greeting = 'Hello';
if (greeting.isNotEmpty) {
print('The string has content!');
}
}
In this example, the string greeting
holds the word “Hello”. The .isNotEmpty
check confirms it has content, so the program prints “The string has content!”. This helps to quickly find out if a string is not empty.
Checking for Strings That Are Just Spaces
Sometimes a string looks empty but actually has spaces. Spaces count as characters, so .isEmpty
alone will say the string is not empty. To check if a string only has spaces, use .trim()
first. This removes spaces at the start and end, then you can check if it’s empty.
void main() {
String input = ' ';
if (input.trim().isEmpty) {
print('The string has only spaces!');
}
}
In this example, input
has three spaces. Using .trim()
removes those spaces, turning it into an empty string. Then .isEmpty
returns true, so the program prints “The string has only spaces!”. This way, you can catch strings that look empty but are full of spaces.
Fun Example: Empty String Game
This example plays a simple game by checking different strings to see if they are empty or not. It looks at each string, trims spaces, then tells you if it’s empty.
void main() {
List<String> inputs = ['', 'Dart', ' ', 'Flutter'];
for (var input in inputs) {
print('Checking "$input": ${input.trim().isEmpty ? 'Empty' : 'Not empty'}');
}
}
Here, we have a list of strings with empty, normal, and space-only values. The program goes through each one, trims spaces, and prints “Empty” if nothing is left, or “Not empty” if there is some text. This is a fun way to understand how trimming and checking works!
Conclusion
Checking for empty strings in Dart is easy and useful. You can use .isEmpty
to see if a string has no characters at all. The .isNotEmpty
property helps you check if a string contains some text. When strings might have spaces, use .trim()
before checking to make sure spaces don’t count as content. These simple tools help keep your code clean and clear.