watchEffect method

void watchEffect(
  1. Function effect, {
  2. required Object? key,
  3. required bool immediate,
  4. required bool once,
})

Implementation

@pragma('vm:prefer-inline')
void watchEffect(
  Function effect, {
  required Object? key,
  required bool immediate,
  required bool once,
}) {
  assert(
    !immediate || key != null,
    'watchEffect(immediate: true) requires a unique key',
  );
  assert(
    !once || key != null,
    'watchEffect(once: true) requires a unique key',
  );

  final oldLength = _selectorsAndEffects.length;
  _selectorsAndEffects.length += 3;
  _selectorsAndEffects[oldLength] = false;
  _selectorsAndEffects[oldLength + 1] = effect;
  _selectorsAndEffects[oldLength + 2] = key;

  if (key != null) {
    final effectBitmask = _keyedEffectStates[key];
    final shouldInvoke = immediate && effectBitmask == null;
    if (shouldInvoke) {
      effect(subscription.callbackArgument);
    }
    _keyedEffectStates[key] = _effectBitmask(
      once: once,
      wasInvoked: shouldInvoke ||
          effectBitmask != null && _effectBitmaskWasInvoked(effectBitmask),
    );
  }
}