how to check two maps are equal in dart
Is it possible to check two maps are equals or not like java equals
?
void main() {
Map map1 = {'size': 38, 'color': 'red'};
Map map2 = {'size': 38, 'color': 'red'};
if(map1== map2){//both keys and values
print('yes');
}else{
print('no');
}
}
I found mapEquals
.
import 'package:flutter/foundation.dart';
void main() {
Map map1 = {'size': 38, 'color': 'red'};
Map map2 = {'size': 38, 'color': 'red'};
if(mapEquals(map1, map2)){
print('yes');
}else{
print('no');
}
}
Use MapEquality().equals(Object a, Object b)
. It will return true
or false
.
import 'package:collection/equality.dart';
MapEquality().equals(map1, map2)
For Flutter, if you have a nested Map and you need to check its equality with another nested object in terms of key and value mappings use :
import 'package:collection/collection.dart';
if(DeepCollectionEquality().equals(map1, map2)) {
print('Maps are equal');
} else {
print('Maps are not equal');
}
For non-Flutter Dart code, package:quiver
provides a mapsEqual
function to compare Map
s (and similar functions for List
s and Set
s).
Any of these answer didn't worked for me.
I tried map1.toString() == map2.toString() and it worked.