{

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


Compare Map of objects for equality  in Dart or Flutter

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

  • 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 they should satisfy the below things

  • key and value pairs in both maps are equal
  • Length of both maps is 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 whether two maps are equal or not

  • using collections MapEquality collections module provides the MapEquality equals() method to 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 is not equal

Similarly, you can also do this with the 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 is not equal
  • using quiver collection

In dart, quiver packages 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 keys 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 an equal comparison result with a 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, Comparing the two maps, returns true if the 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 whether 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 returns 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 maps 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

Perl How to: Find a Given Variable String is numeric or not

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

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

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

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart