addListener method

  1. @override
ProviderSubscription<StateT> addListener(
  1. Node node,
  2. void listener(
    1. StateT? previous,
    2. StateT next
    ), {
  3. required void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  4. required void onDependencyMayHaveChanged()?,
  5. required bool fireImmediately,
})
override

Starts listening to this transformer

Implementation

@override
ProviderSubscription<StateT> addListener(
  Node node,
  void Function(StateT? previous, StateT next) listener, {
  required void Function(Object error, StackTrace stackTrace)? onError,
  required void Function()? onDependencyMayHaveChanged,
  required bool fireImmediately,
}) {
  onError ??= Zone.current.handleUncaughtError;

  final element = node.readProviderElement(this);

  element.flush();
  if (fireImmediately) {
    handleFireImmediately(
      element.getState()!,
      listener: listener,
      onError: onError,
    );
  }

  // Calling before initializing the subscription,
  // to ensure that "hasListeners" represents the state _before_
  // the listener is added
  element._onListen();

  return _ProviderStateSubscription<StateT>(
    node,
    listenedElement: element,
    listener: (prev, next) => listener(prev as StateT?, next as StateT),
    onError: onError,
  );
}