effect<T> function

EffectRunner<T> effect<T>(
  1. T runner(), {
  2. void scheduler()?,
  3. void onStop()?,
})

Creates and runs an effect.

  • runner is the function to run as the effect.
  • scheduler is an optional function to schedule the effect run.
  • onStop is an optional function called when the effect is stopped.
final count = ref(0);

effect(() {
    print('Count: ${count.value}');
});

count.value = 10; // prints 'Count: 10'

Implementation

public.EffectRunner<T> effect<T>(
  T Function() runner, {
  void Function()? scheduler,
  void Function()? onStop,
}) {
  final effect = impl.Effect(
    runner,
    scheduler: scheduler,
    onStop: onStop,
  );

  try {
    effect.run();
  } catch (_) {
    effect.stop();
    rethrow;
  }

  return impl.EffectRunner(effect);
}