Dart/Flutter - Using String toUpperCase() and toLowerCase() Examples

This tutorial shows you how to use String's toUpperCase and toLowerCase methods in Dart/Flutter.

Using String toUpperCase()

Dart's String class has an instance method upperCase().

  String toUpperCase();

It is used to convert all characters to upper case. In other words, this method is used to capitalize all characters of a string. As it uses language independent Unicode mapping, it doesn't work properly in some languages.

This method creates a new string because strings are immutable in Dart.

  String text = 'www.Woolha.com';
  print(text.toUpperCase()); // WWW.WOOLHA.COM
  print(text); // www.Woolha.com

If the passed string is null or hasn't been assigned, it will throw an error.

  String text;
  print(text.toUpperCase()); // NoSuchMethodError: The method 'toUpperCase' was called on null.

Using String toLowerCase()

Dart's String class has an instance method toLowerCase().

  String toLowerCase();

It is used to convert all characters to lower case. Like toUpperCase(), it doesn't work properly in some languages since it uses language independent Unicode mapping.

This method creates a new string because strings are immutable in Dart.

  String text = 'www.Woolha.com';
  print(text.toLowerCase()); // www.woolha.com
  print(text); // www.Woolha.com

An error will be thrown if the passed string is null or hasn't been assigned.

  String text;
  print(text.toLowerCase()); // NoSuchMethodError: The method 'toLowerCase' was called on null.