Skip to content
pubscout

weak_map

4.1.3, released 3 days ago by glasberg.dev

A map whose keys are weakly held, and cache functions for memoization.

WASM-ready
$ flutter pub add weak_map
Source repository

What it does

Provides a WeakMap where keys are held weakly to attach data to objects without preventing garbage collection. Also includes cache functions for memoizing expensive calculations over immutable state.

AI-written summary of version 4.1.2, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to attach metadata or cached results to objects you don't control without causing memory leaks
  • You want to memoize expensive computations over immutable state that should be discarded when state changes or is no longer used

Skip it when

  • You need to iterate over entries or know the length of the map
  • You are using mutable state for caching computations

Strengths

  • Prevents memory leaks by not keeping keys alive
  • Built-in memoization and cache functions for immutable states
  • Pure Dart with zero dependencies

Weaknesses

  • No iteration, length, keys, or values methods on WeakMap by design
  • Requires immutable state objects for proper cache invalidation

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
0

not documented

var map = WeakMap<User, Avatar>();
map[user] = avatar;
var a = map[user];

Alternatives

Packages that do the same job and could replace weak_map.

Works with

Packages commonly used together with weak_map: ones it builds on, ones built on it, and its own family.