operator [] method

  1. @override
V? operator [](
  1. Object? key
)
override

The value for the given key, or null if key is not in the map.

Some maps allow null as a value. For those maps, a lookup using this operator cannot distinguish between a key not being in the map, and the key being there with a null value. Methods like containsKey or putIfAbsent can be used if the distinction is important.

Implementation

@override
V? operator [](Object? key) {
  if (key is K) {
    var current = _root;
    // Navigate to the right node.
    for (final part in _getParts(key)) {
      final node = current.getChild(part);
      if (node == null) {
        return null;
      }
      current = node;
    }
    // Verify we found a value node.
    if (current.hasKeyAndValue) {
      return current.value;
    }
  }
  return null;
}