Computed<T> class

A class that represents a derived value computed from one or more reactive sources.

The Computed class automatically tracks dependencies on Reactive instances and recalculates its value whenever any of its sources change. This enables creating a reactive dependency graph where changes propagate automatically.

Key features:

  • Automatically updates when source values change
  • Supports multiple reactive sources
  • Provides both synchronous value access and reactive stream
  • Supports middleware for customizing behavior
  • Offers batch update control for optimizing multiple updates

Example usage:

// Create source reactive values
final firstName = Reactive('John');
final lastName = Reactive('Doe');

// Create a computed value that depends on both sources
final fullName = Computed([firstName, lastName], (sources) {
  return '${sources[0].value} ${sources[1].value}';
});

// Access the computed value
print(fullName.value); // "John Doe"

// When a source changes, the computed value updates automatically
firstName.value = 'Jane';
print(fullName.value); // "Jane Doe"

You can also attach middleware to a Computed instance to customize its behavior:

final formattedName = Computed([firstName, lastName], (sources) {
  return '${sources[0].value} ${sources[1].value}';
}, middlewares: [LoggingMiddleware<String>()]);

Constructors

Computed(List<Reactive> _sources, T _compute(List<Reactive>), {List<FluxivityMiddleware<T>>? middlewares})
Constructs a new Computed instance that derives its value from reactive sources.

Properties

hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sources List<Reactive>
Returns the list of source Reactive instances that this computed value depends on.
no setter
stream Stream<Snapshot<T>>
Returns a stream of snapshots containing the computed value.
no setter
value → T
Returns the current computed value.
no setter

Methods

dispose() → void
Releases resources used by this computed 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 computed instance.
toString() String
A string representation of this object.
override

Operators

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