The string is the data type applied to variables. And, String variables stores a group of characters enclosed in double("") and single(''
) quotes.
The default value is null
if it does not assign a string value in Dart and Flutter.
There is a difference between null, empty, and blank Strings in Dart and Flutter.
- null string is variable is declared, but not assigned with a value. An example is
String value;
and the value isnull
. - Empty String is a string assigned with an empty string, and the length of a string is zero. Example syntax is
String value=""
; - Blank String is a string assigned with at least whitespace characters, and the length of a string is greater than zero. Example syntax is
String value=" "
;
This tutorial shows multiple ways to check a string is empty, null, or blank in Dart and Flutter example.
Check String is empty, null, in Dart and Flutter
In Dart, There are multiple ways to check a given string is empty, null, or Blank.
use isEmpty and isNotEmpty method
The string has inbuilt properties
- isEmpty: Returns true if String is empty or null, and It returns false for blank characters
- isNotEmpty: Returns true if the string is not empty or non-null, and It returns false for empty and null.
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 nullable type operator (Question mark after ) for a variable throws 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
So, these methods do not apply to nullable type operators(?
). And also length property throws the same error.
Another way to check using string.length
use string length
dart
string package has a length that returns the length of a given string.
Syntax:
int get length
Returns integer number 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 String contains blank characters in dart and flutter
isEmpty returns false for the blank string that contains white space characters.
use String trim()
method and call isBlank
as given below.
void main() async {
String str1 = " ";
print(str1.trim().isEmpty); //true
}
Conclusion
Learned how to check a given string is empty or null or blank white space characters in a dart or flutter programming with examples