'Debugger calling functions untimely. How to disable variable evaluation?

The Dart debugger is apparently calling collection methods to display values in the Variables view. The result is that the execution is completely different in Debug mode than in Run mode.

In the following example, Run prints 2, while Debug prints 8 or 14 or 20 depending on the breakpoints...

How can I prevent this?

import 'dart:collection';

main() {
  var test = CustomMap();
  test.addAll({1: 1, 2: 2, 3: 3});
  test[1];
  test[2];
  print(test.callCount);
}

class CustomMap extends MapBase<int, int> {
  var _map = Map<int, int>();
  int callCount = 0;

  int? operator [](Object? key) {
    callCount++;
    return _map[key];
  }

  void operator []=(int key, int value) => _map[key] = value;

  void clear() {}

  Iterable<int> get keys => _map.keys;

  int? remove(Object? key) => _map.remove(key);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source