swap method Null safety

Map<V, K> swap()

Returns a map with the keys and values swapped. If there are duplicate keys the function throws an argument error (So use with containsDuplicateValues()).

Implementation

Map<V, K> swap() {
  Map<V, K> newMap = {};
  Set<V> duplicateChecker = Set.from(values);
  if (duplicateChecker.length < length) {
    throw ArgumentError('There are duplicate'
        'values in the Maps values $values. Keys need to be unique therefore the swap could\'t happen');
  }
  forEach((key, value) {
    newMap[value] = key;
  });
  return newMap;
}