get<T> method

  1. @override
  2. @visibleForTesting
  3. @protected
T get<T>({
  1. String? name,
  2. bool addToEventStream = true,
})

Retrieves a state value of type T from the container.

This method provides type-safe access to a previously registered state value.

T The type of the state value to retrieve.

name An optional name identifier for the state value. Used when multiple state values of the same type need to be distinguished.

addToEventStream Whether to emit a GetEvent to the event stream when the stream is open. Defaults to true. Set to false to access state silently without notifying event stream listeners.

Returns the current value of the requested state of type T.

Throws an ArgumentError if the state of type T with the given name has not been registered.

Implementation

@override
@visibleForTesting
@protected
T get<T>({String? name, bool addToEventStream = true}) {
  if (!_hasKey<T>(name)) {
    throw ArgumentError(
      'Piece of state of type "$T" ${name != null ? 'and name "$name" ' : '(no name) '}not found. You must register it in the constructor of your Hako class before trying to get it.',
    );
  }
  final state = _stateMap[(T, name)] as T;
  if (addToEventStream) {
    onEvent(GetEvent<T>(name: name, state));
  }
  return state;
}