addEffect method

StreamSubscription<Snapshot<Map<K, V>>> addEffect(
  1. dynamic effect(
    1. Snapshot<Map<K, V>>
    )
)

Adds an effect that runs whenever the map changes.

An effect is a function that gets called when the map changes. It receives a Snapshot containing both the old and new map values.

Example:

final profile = {'name': 'Alice'}.reactive;

// Add an effect to log changes
profile.addEffect((snapshot) {
  print('Profile changed:');
  snapshot.newValue.forEach((key, value) {
    if (snapshot.oldValue[key] != value) {
      print('  $key: ${snapshot.oldValue[key]} -> $value');
    }
  });
});

profile.value['age'] = 28;
profile.value = Map.from(profile.value); // Effect will trigger

Note: To make modifications trigger effects, you must assign the modified map back to the reactive value, typically using value = Map.from(value).

Implementation

StreamSubscription<Snapshot<Map<K, V>>> addEffect(
    Function(Snapshot<Map<K, V>>) effect) {
  return stream.listen(effect);
}