deepDifferenceByKey method

Map deepDifferenceByKey(
  1. Map toCompare

Returns new instance of Map containing values, which key differs in toCompare. Does not work with nested List and Set yet.

According to complement in set theory (for clarity see Venn Diagrams): A - this B - toCompare Difference in keys (relative complement): A \ B


Map deepDifferenceByKey(Map toCompare) {
  var diff = {};
  forEach((key, value) {
    if (!toCompare.containsKey(key)) {
      diff.addEntries([MapEntry(key, value)]);
    } else if (value is Map && toCompare[key] is Map) {
      var deepDiff = value.deepDifferenceByKey(toCompare[key]);
      if (deepDiff.isNotEmpty) {
        diff.addEntries([MapEntry(key, deepDiff)]);
    } else if (value is Map) {
      diff.addEntries([MapEntry(key, value)]);

  return diff;