Effect<Value> class
sealed
Describes a unit of work that can emit values, be cancelled, or compose with other units to form more elaborate workflows.
Example:
final loadUser = Effect.task(() async => await api.fetchUser());
final cancellable = loadUser.cancellable(id: "user");
final debounced = cancellable.debounce(
id: "user",
interval: const Duration(milliseconds: 300),
);
Constructors
-
Effect.asyncPeriodic(Duration interval, Future<
Value> computation(int computationCount), {Value onDone()?, Value onError(Object error, StackTrace stackTrace)?, bool? cancelOnError}) -
Asynchronous counterpart to Effect.periodic. The
computationcan perform suspending work before yielding each value.factory - Effect.cancel({required EffectID id})
-
Requests cancellation for any in-flight effect registered with
id. This is the counterpart to Effect.cancellable.constfactory -
Effect.concatenate({required Iterable<
Effect< effects})Value> > -
Executes the supplied
effectssequentially. Each effect starts only after the previous one completes, preserving deterministic ordering.constfactory - Effect.delayed(Duration duration, Value computation())
-
Delays executing
computationbydurationusing the default scheduler and emits the returned value once the delay completes.factory -
Effect.merge({required Iterable<
Effect< effects})Value> > -
Runs every effect in
effectsconcurrently and merges their emissions into a single effect. Completion occurs once all nested effects finish.constfactory - Effect.none()
-
Represents the absence of work. Running this effect completes
immediately without emitting values or registering cancellations.
constfactory
- Effect.periodic(Duration interval, Value computation(int computationCount), {Value onDone()?, Value onError(Object error, StackTrace stackTrace)?, bool? cancelOnError})
-
Emits a value on every
intervalusing the synchronouscomputation. Optional callbacks allow consumers to provide completion or error values, or to opt into canceling the subscription when an error occurs.factory - Effect.run(Value runner(), {Value onError(Object error, StackTrace stackTrace)?})
-
Runs the synchronous
runnerimmediately when the effect is executed and emits its return value. Errors can be transformed viaonError.constfactory -
Effect.stream({required Stream<
Value> stream, Value onDone()?, Value onError(Object error, StackTrace stackTrace)?, bool? cancelOnError}) -
Bridges any Dart Stream into the effect system. Emissions, completion,
and errors are forwarded through the provided callbacks.
constfactory
-
Effect.streamTask(Stream<
Value> stream(), {Value onDone()?, Value onError(Object error, StackTrace stackTrace)?, bool cancelOnError = true}) -
Lazily constructs and subscribes to a stream returned by
streameach time the effect runs. Useful when each invocation requires a new stream subscription.factory -
Effect.task(Future<
Value> future(), {bool runInIsolate, Value onError(Object error, StackTrace stackTrace)?}) -
Wraps an asynchronous computation. The
futurebuilder is invoked when the effect runs, optionally inside a new isolate. Errors can be mapped to fallback values viaonError.constfactory - Effect.value(Value value)
-
Emits
valuesynchronously when the effect is run. This is useful for sending immediate feedback without scheduling asynchronous work.constfactory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancellable(
{required EffectID id, bool cancelInFlight = false}) → Effect< Value> -
Associates this effect with
idso that it can be cancelled later. SettingcancelInFlighttotruecancels any currently running effect that uses the same identifier before starting this one. -
concatenate(
{required Effect< Value> effect}) → Effect<Value> -
Convenience helper for sequencing this effect with
effect. Both effects run one after the other, preserving emission order. -
debounce(
{required String id, required Duration interval}) → Effect< Value> -
Debounces emissions by delaying them until no new values have arrived for
the specified
interval. Uses the default scheduler for timing. -
debounceWithScheduler<
Time, Interval> ({required String id, required Interval interval, required Scheduler< Time, Interval> scheduler}) → Effect<Value> -
Scheduler-aware variant of Effect.debounce that uses the provided
scheduler. The sameidis reused to cancel pending debounced work. -
delay(
Duration duration) → Effect< Value> -
Delays emissions by
durationon the default scheduler, allowing debounced or scheduled work without manually handling timers. -
delayWithScheduler<
Time, Interval> (Interval interval, Scheduler< Time, Interval> scheduler) → Effect<Value> -
Scheduler-aware delay helper that uses the provided
schedulerto pause forintervalbefore running the wrapped effect. -
fireAndForget<
NewValue> () → Effect< NewValue> - Runs the effect but suppresses any emitted values. Useful for fire-and- forget operations where only the side effects matter.
-
flatMap<
NewValue> (Effect< NewValue> mapper(Value value)) → Effect<NewValue> -
For each emitted value, invokes
mapperto create another effect and flattens the resulting sequence into a single stream of values. -
map<
NewValue> (NewValue mapper(Value value)) → Effect< NewValue> -
Transforms emitted values using
mapperbefore forwarding them to the reducer or downstream effect handler. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
EffectHandler< Value> handler) → Future<void> -
Executes the effect immediately with the supplied
handler. Each effect implementation defines how it schedules work, emits values, and registers disposables. -
throttle(
{required EffectID id, required Duration interval, ThrottleDirection direction = ThrottleDirection.leading, bool emitFirst = true}) → Effect< Value> -
Throttles emissions so that values are produced no more frequently than
interval. The default scheduler controls the timing, anddirectiondetermines whether leading or trailing values are emitted. -
throttleWithScheduler<
Time, Interval> ({required EffectID id, required Interval interval, required Scheduler< Time, Interval> scheduler, required ThrottleDirection direction, required bool emitFirst}) → Effect<Value> -
Scheduler-aware throttle helper that allows custom timing via
schedulerand fine-grained control over leading/trailing emission behavior. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
delayedWithScheduler<
Value, Time, Interval> (Interval interval, Value computation(), Scheduler< Time, Interval> scheduler) → Effect<Value> -
Scheduler-aware variant of Effect.delayed that allows a custom
schedulerandintervalrepresentation.