Reactive<T> class

A reactive container that holds a value and notifies listeners when it changes.

The Reactive class is the foundation of Fluxivity's reactivity system. It wraps a value and provides mechanisms to observe changes to that value. When the value changes, all listeners are notified of the change.

Key features:

  • Holds a value of type T that can be accessed synchronously with value
  • Provides a stream of updates that emits a Snapshot whenever the value changes
  • Supports addEffect for side-effect handling
  • Can be wrapped with FluxivityMiddleware for additional behaviors
  • Supports batched updates to optimize multiple changes

Example usage:

// Create a reactive value
final counter = Reactive(0);

// Access the current value
print(counter.value); // 0

// Listen for changes
counter.stream.listen((snapshot) {
  print('Counter changed from ${snapshot.oldValue} to ${snapshot.newValue}');
});

// Update the value
counter.value = 1; // Triggers notification: "Counter changed from 0 to 1"

Reactive instances are often used as sources for Computed values:

final doubledCounter = Computed([counter], (sources) {
  return sources[0].value * 2;
});
Available extensions

Constructors

Reactive(T _value, {List<FluxivityMiddleware<T>>? middlewares})

Properties

hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stream Stream<Snapshot<T>>
Returns a stream of snapshots containing the old and new values after each change.
no setter
value ↔ T
Gets the current value of this reactive instance.
getter/setter pair

Methods

addEffect(dynamic effect(Snapshot<T>)) StreamSubscription<Snapshot<T>>
Adds an effect function that gets called whenever the value changes.
addEffect(dynamic effect(Snapshot<List<E>>)) StreamSubscription<Snapshot<List<E>>>

Available on Reactive<List<E>>, provided by the ReactiveListHelpers extension

Adds an effect that runs whenever the list changes.
addEffect(dynamic effect(Snapshot<Map<K, V>>)) StreamSubscription<Snapshot<Map<K, V>>>

Available on Reactive<Map<K, V>>, provided by the ReactiveMapHelpers extension

Adds an effect that runs whenever the map changes.
addEffect(dynamic effect(Snapshot<Set<E>>)) StreamSubscription<Snapshot<Set<E>>>

Available on Reactive<Set<E>>, provided by the ReactiveSetHelpers extension

Adds an effect that runs whenever the set changes.
dispose() → void
Releases resources used by this reactive instance.
endBatchUpdate({bool publishAll = false}) → void
Ends a batch update session and optionally publishes the buffered changes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
startBatchUpdate() → void
Starts a batch update session for this reactive instance.
toString() String
A string representation of this object.
override
unwrap() List<E>

Available on Reactive<List<E>>, provided by the ReactiveListHelpers extension

Returns the non-reactive list value.
unwrap() Map<K, V>

Available on Reactive<Map<K, V>>, provided by the ReactiveMapHelpers extension

Returns the non-reactive map value.
unwrap() Set<E>

Available on Reactive<Set<E>>, provided by the ReactiveSetHelpers extension

Returns the non-reactive set value.

Operators

operator ==(Object other) bool
The equality operator.
override