FlutterEffectImpl constructor

FlutterEffectImpl(
  1. void fn(), {
  2. bool lazy = false,
  3. JoltDebugFn? onDebug,
})

Creates a new Flutter effect with the given function.

Parameters:

  • fn: The effect function to execute
  • lazy: Whether to run the effect immediately upon creation. If true, the effect will execute once immediately when created, then automatically re-run whenever its reactive dependencies change. If false (default), the effect will only run when dependencies change, not immediately upon creation.
  • onDebug: Optional debug callback for reactive system debugging

The effect function will be called at the end of the current Flutter frame, batching multiple triggers within the same frame into a single execution.

Example:

final signal = Signal(0);

// Effect runs at end of frame when signal changes
final effect = FlutterEffect(() {
  print('Signal value: ${signal.value}');
});

signal.value = 1;
signal.value = 2;
// Effect executes once at end of frame with signal.value = 2

Implementation

FlutterEffectImpl(this.fn, {bool lazy = false, JoltDebugFn? onDebug})
    : super(flags: ReactiveFlags.watching | ReactiveFlags.recursedCheck) {
  JoltDebug.create(this, onDebug);

  final prevSub = getActiveSub();
  if (prevSub != null) {
    link(this, prevSub, 0);
  }

  if (!lazy) {
    final prevSub = setActiveSub(this);
    try {
      _effectFn();
    } finally {
      setActiveSub(prevSub);
      flags &= ~ReactiveFlags.recursedCheck;
    }
  } else {
    flags &= ~ReactiveFlags.recursedCheck;
  }
}