Dart| Flutter How to: Check strings are equal or not Example

This tutorial shows how to compare two strings are equal or not Dart or Flutter.

How to Check strings are equal or not in Dart and Flutter

Dart does not provide the isEquals (java) method to compare methods for equality.

`String equals compare the same characters in the order of both strings are equal.

Since, String are immutable objects, Once you create a string object, does not allow you to modify the string, However, if you append to an existing string, it creates a new string with result, the Original string also exists in memory.

String values of the same value create two references for the same string object.

For example, Given two strings

  String str1 = 'one';
  String str2 = 'two';
  String str3 = 'one';

Above the program, two objects (one, two) are created in memory, str1 and str3 are referred to the same object (one), str2 reference is pointed to another object(two).

String equality check two string values are equal (str1,str2) returns true.

It provides two ways to check string’s values are equal or not.

  • double equal operator(==): Returns true if both strings are the same set of characters, Else return false
  • compareTo method :
    • returns 0 if both strings are equal, else returns non zero number
    • returns negative number(-1) if first is ordered before second string
    • returns positive number(1), if first is ordered after second string.

Similarly, two strings references are checked using the identical method which checks two references pointed to the same object in memory.

How to compare if Strings are equal or not in dart/flutter?

== checks two string object holds the same set of character Since it returns boolean values, useful in a conditional expression such as if.

compareTo() function always returns numeric value, again one more step (result==0)required to check for string equality.

Both ways It does not check for string null safety.

identical() in string checks two variables hold the same object in memory or not.

void main() {
  print("one" == "one"); // true
  print("one" == "ONE"); // false
  print("one".compareTo("one")); // 0
  print("one".compareTo("two")); // -1
  String str1 = 'one';
  String str2 = 'two';
  String str3 = 'one';
  print(identical(str1, str2)); // false
  print(identical(str1, str3)); // true


To summarize, We can easily compare strings using double equal, compareTo, and identical methods.

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