{

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


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 a variable that is declared but not assigned with a value. An example is String value; and the value is null.
  • 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 whether 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 whether 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 a string.length

use string length

The dart string package has a length that returns the length of a given 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 String contains blank characters in dart and flutter

isEmpty returns are 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 whether a given string is empty or null or blank white space characters in a dart or flutter programming with examples

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example