weak_map 3.0.1 copy "weak_map: ^3.0.1" to clipboard
weak_map: ^3.0.1 copied to clipboard

WeakMap is a map where the keys are weakly referenced. WeakContainer lets you check if an object is the same you had before. Cache functions for memoization with weak-references.

example/main.dart

import 'package:weak_map/weak_map.dart';

void main() {
  var map = WeakMap<String, int?>();

  // 1)
  print('\nmap["A"] = 1');
  map["A"] = 1;
  print('A = ${map["A"]}'); // A = 1
  print('A exists in map = ${map.contains("A")}'); // A = true

  // 2)
  print('\nDoes not set B');
  print('B = ${map["B"]}'); // B = null
  print('B exists in map = ${map.contains("B")}'); // A = false

  // 3)
  print('\nmap["A"] = null');
  map["A"] = null;
  print('A = ${map["A"]}'); // A = null
  // Adding some null value to the map is the same as removing the key.
  print('A exists in map = ${map.contains("A")}'); // A = false

  // 4)
  print('\nvalue = WeakContainer("X")');
  var value = WeakContainer("X");
  print('Contains X = ${value.contains("X")}'); // Contains X = true
  print('Contains Y = ${value.contains("Y")}'); // Contains Y = false
}
16
likes
130
pub points
89%
popularity

Publisher

verified publisherglasberg.dev

WeakMap is a map where the keys are weakly referenced. WeakContainer lets you check if an object is the same you had before. Cache functions for memoization with weak-references.

Repository (GitHub)
View/report issues

Topics

#memory #memory-management #cache #collections #data-structures

Documentation

API reference

License

BSD-2-Clause (LICENSE)

More

Packages that depend on weak_map