{

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 variable 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 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

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: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example