{

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

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example