{

Dart/Flutter:How to Check two Maps for equal or not


This tutorial shows multiple examples of how to compare two maps for equal or not in Dart and Flutter programming.

Compare Map of objects for equality  in Dart or Flutter
  • Compare Two maps equal in dart and flutter
  • Check for equal comparison of nested maps in dart and flutter

The map contains key and value pairs.

If two maps are equal, then it should satisfy the below things

  • key and value pairs in both maps are equal
  • Length of both maps are equal
  • Order of keys and values is also not important.

How to check if maps are equal or not in Dart?

There are multiple ways to compare two maps are equal or not

  • using collections MapEquality collections module provides MapEquality equals() method compare two plain maps and returns true or false.

    It does not support deeply nested map structures.

Here is an example code

import 'package:collection/collection.dart';

void main() {
  Map map1 = {'id': 1, 'name': 'john'};
  Map map2 = {'id': 2, 'name': 'eric'};
  Map map3 = {'id': 1, 'name': 'john'};
  print(MapEquality().equals(map1, map2)); // false
  print(MapEquality().equals(map1, map3)); // true
  if (MapEquality().equals(map1, map2)) {
    print('map are equal');
  } else {
    print('map are not equal');
  }
}

Output:

false
true
map are not equal

Similarly, you can also do with DeepCollectionEquality().equals() method.

import 'package:collection/collection.dart';

void main() {
  Map map1 = {'id': 1, 'name': 'john'};
  Map map2 = {'id': 2, 'name': 'eric'};
  Map map3 = {'id': 1, 'name': 'john'};
  print(DeepCollectionEquality().equals(map1, map2)); // false
  print(DeepCollectionEquality().equals(map1, map3)); // true
  if (DeepCollectionEquality().equals(map1, map2)) {
    print('map are equal');
  } else {
    print('map are not equal');
  }
}

Output:

false
true
map are not equal
  • using quiver collection

In dart, quiver package a library for collection utility reusable functions.

It has a mapsEqual function that compares two maps and returns true, if equal, else not equal.

Syntax:

bool mapsEqual(Map? a, Map? b) 

It takes two maps for comparison of key and values are equal or not Return value: true or false.

Here is an example code

import 'package:quiver/collection.dart';

void main() {
  Map map1 = {'id': 1, 'name': 'john'};
  Map map2 = {'id': 2, 'name': 'eric'};
  Map map3 = {'name': 'john', 'id': 1};
  print(mapsEqual(map1, map2)); //false
  print(mapsEqual(map1, map3)); //true

  if (mapsEqual(map1, map2)) {
    print('map1 and  map2 are equal');
  } else {
    print('map1 and  map2 are not equal');
  }
}

How to check if Maps are equal or not in Flutter

flutter/foundation.dart has a mapEquals method, does compare each element key and value pairs in the map, and returns equal comparison result with boolean type.

bool mapEquals<T>(Map<T>? a, Map<T>? b)
  • true: Returns true if two maps are equal with ignoring the order of elements.
  • false: two maps are not equal.

Here, Compare the two maps, returns true if two maps are equal.

Here is an example of equality for a maps

import 'package:flutter/foundation.dart';

void main() {
  Map map1 = {'id': 1, 'name': 'john'};
  Map map2 = {'id': 2, 'name': 'eric'};
  Map map3 = {'id': 1, 'name': 'john'};
  print(mapEquals(map1, map2)); // false 
  print(mapEquals(map1, map3)); // true
  if (mapEquals(map1, map2)) {
    print('map are equal');
  } else {
    print('map are not equal');
  }
}

How to compare nested maps for equal or not in dart and flutter

This example checks two nested maps are equal or not. In this example,

  • Created two nested maps.

  • First checked two maps for comparison using MapEquality().equals(). It returns false as it only checks one level of map structure.

  • DeepCollectionEquality().equals() checks nested maps and compare two maps and return true if nested maps are equal, else return false

import 'package:collection/collection.dart';

void main() {
  Map map1 = {
    'role': {'name': 'sales', 'id': 1}
  };
  Map map2 = {
    'role': {'name': 'admin', 'id': 2}
  };
  Map map3 = {
    'role': {'name': 'sales', 'id': 1}
  };
  print(MapEquality().equals(map1, map2)); // false
  print(MapEquality().equals(map1, map3)); // false
  print(DeepCollectionEquality().equals(map1, map2)); // false
  print(DeepCollectionEquality().equals(map1, map3)); // true
  if (DeepCollectionEquality().equals(map1, map2)) {
    print('nested map are equal');
  } else {
    print('nested map are not equal');
  }
}

Output:

false
false
false
true
nested map are not equal

Conclusion

In this tutorial, Learned how to compare two maps and nested maps for comparison and equality in Dart and flutter programming

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