Tree
Tree

Reputation: 31371

Removing elements from a Map

How to make iteration on the map that will remove elements on by one and then call one function after each element has been removed?

lets say I have release function

  final HashMap<int, bool> _instances = new HashMap<int, bool>();

  void release(dynamic instance) {
    if (_instances[instance.hashCode] != null) {
      _releaseHashCode(instance.hashCode);
      _dispatcher.dispatchEvent(PinEvent.release);
    }
  }

  void _releaseHashCode(int hashCode) => _instances.remove(hashCode);

Now I would like to create releaseAll() that will call _releaseHashCode for each element in the map, thus releasing it and dispatching release event.

  void releaseAll() {
    ...
  }

Upvotes: 1

Views: 7904

Answers (2)

amorenew
amorenew

Reputation: 10906

Check removeWhere

final Map<String, dynamic> tempMap = json.decode(jsonValue);
tempMap.removeWhere((String key, dynamic value)=> value==null);

Upvotes: 12

Tree
Tree

Reputation: 31371

This is how I did it

  void releaseAll() {
    var tempMap = new HashMap<int, bool>()..addAll(_instances);
    tempMap.forEach((int hashCode, bool value) {
      _releaseHashCode(hashCode);
      _dispatcher.dispatchEvent(PinEvent.release);
    });
    tempMap.clear();
    tempMap = null;
  }

Upvotes: 0

Related Questions