Hako class abstract
A container for multiple state objects.
Hako is the main class that users should extend to create their own state containers. It provides methods to get and set state values in a type-safe manner, while also tracking state changes for reactivity.
Example:
class CounterHako extends Hako {
CounterHako() : super((register) {
register<int>(0); // Register initial state
});
void increment() => set<int>((current) => current + 1);
int get count => get<int>();
}
Hako classes provide the foundation for creating state containers with automatic change tracking and event emission capabilities.
All state values must be registered during construction and can then be safely accessed and modified throughout the container's lifecycle.
Lifecycle
- Registration: All state values must be declared upfront in the constructor using the provided registrar callback
- Usage: Access state with
get<T>()and update withset<T>() - Disposal: Call
dispose()to clean up resources when no longer needed
Event stream
Hako classes include an integrated event system that emits events for state operations when the event stream is open. This enables testing, monitoring, debugging, and any other possible reactive behaviors:
- GetEvent: Emitted when state is accessed via
get<T>() - SetEvent: Emitted when state is modified via
set<T>()
Events are only emitted when isEventStreamOpen returns true. The event
stream can be controlled through openEventStream and closeEventStream
methods, and is automatically closed when dispose() is called.
Example usage for testing or debugging:
final hako = MyHako();
final stream = hako.openEventStream();
stream.listen((event) {
if (event is SetEvent<int>) {
print('Counter changed from ${event.previous} to ${event.current}');
}
});
Type safety
All state operations are type-safe and require explicit type parameters.
State values should be registered with their exact types during
construction, and subsequent access must use the same types.
Nullable types must be explicitly declared (e.g., register<String?>(null)
instead of register(null)).
Optional names
State values can be registered with optional string names to allow multiple instances of the same type within a single Hako container. This is useful when you need to store several values of the same type that serve different purposes.
The combination of type and name creates a unique key that identifies each piece of state within the container. This key is used consistently across all operations - you must use the same type and name combination when registering, accessing, and modifying a specific state value.
Unnamed state (where name is null) and named state of the same type
are treated as completely separate and independent state values:
// no issues with the following:
CounterHako() : super((register) {
register<int>(0);
register<int>(0, name: 'count');
});
Error handling
- Attempting to register the same state type and name combination twice throws a StateError
- Accessing or modifying unregistered state throws an ArgumentError
- Registering null values with generic types (Null or dynamic) throws an AssertionError
Constructors
- Hako(void registrar(RegisterCallback register))
- Creates a new Hako instance.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEventStreamOpen → bool
-
Returns whether the event stream is currently open.
no setterinherited
- onSetCalled ← VoidCallback?
-
no getterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
closeEventStream(
) → void -
Closes the current event stream and cleans up resources.
inherited
-
dispose(
) → void -
Disposes of resources associated with this Hako instance.
inherited
-
get<
T> ({String? name, bool addToEventStream = true}) → T -
Retrieves a state value of type
Tfrom the container.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onEvent(
HakoEvent event) → void -
Handles a HakoEvent by adding it to the event sink if one is available.
inherited
-
openEventStream(
) → Stream< HakoEvent> -
Creates and returns a stream of Hako events.
inherited
-
set<
T> (T updater(T current), {String? name, bool addToEventStream = true}) → void -
Updates a state value of type
Tin the container.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited