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< ? middlewares})T> >
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< )) → StreamSubscription<E> >Snapshot< List< >E> > -
Available on Reactive<
Adds an effect that runs whenever the list changes.List< , provided by the ReactiveListHelpers extensionE> > -
addEffect(
dynamic effect(Snapshot< Map< )) → StreamSubscription<K, V> >Snapshot< Map< >K, V> > -
Available on Reactive<
Adds an effect that runs whenever the map changes.Map< , provided by the ReactiveMapHelpers extensionK, V> > -
addEffect(
dynamic effect(Snapshot< Set< )) → StreamSubscription<E> >Snapshot< Set< >E> > -
Available on Reactive<
Adds an effect that runs whenever the set changes.Set< , provided by the ReactiveSetHelpers extensionE> > -
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<
Returns the non-reactive list value.List< , provided by the ReactiveListHelpers extensionE> > -
unwrap(
) → Map< K, V> -
Available on Reactive<
Returns the non-reactive map value.Map< , provided by the ReactiveMapHelpers extensionK, V> > -
unwrap(
) → Set< E> -
Available on Reactive<
Returns the non-reactive set value.Set< , provided by the ReactiveSetHelpers extensionE> >
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override