NeuronAtom<T> class
A modern, lightweight reactive value container.
NeuronAtom is the foundation of Neuron's reactive system. It holds a
value of type T and notifies listeners when the value changes. Think of
it as a smarter version of Flutter's ValueNotifier with additional features.
Core Features
- Value observation: Listeners are notified when value changes
- Equality checking: Custom equality to control when listeners fire
- Value guards: Transform or validate values before emission
- Initial value: Access original value via initialValue
- Previous value: Track changes via previousValue
- Lifecycle hooks: onActive and onInactive for resource management
- RAII Finalizer: Automatically calls dispose if an atom is garbage collected
Basic Usage
final counter = NeuronAtom<int>(0);
// Listen to changes
final cancel = counter.subscribe(() {
print('New value: ${counter.value}');
});
counter.value = 1; // Prints: New value: 1
counter.value = 1; // No print (value unchanged)
counter.value = 2; // Prints: New value: 2
cancel(); // Stop listening
Custom Equality
Control when listeners are notified:
final user = NeuronAtom<User>(
User(id: 1),
equals: (a, b) => a.id == b.id, // Only notify if ID changes
);
Value Guards
Transform or validate values before emission:
final percentage = NeuronAtom<int>(
50,
guard: (current, next) => next.clamp(0, 100), // Ensure 0-100 range
);
percentage.value = 150; // Actually sets to 100
Selective Listening
Create derived atoms that only update for specific changes:
final user = NeuronAtom<User>(User(name: 'Alice', age: 30));
final nameOnly = user.select((u) => u.name); // Only fires on name change
Note: For most use cases, use Signal<T> instead, which extends NeuronAtom with stream support and controller binding.
See also:
- Signal - Preferred reactive value with stream support
- AsyncSignal - For async operations
- Computed - For derived values
- Implemented types
- Implementers
- Available extensions
Constructors
- NeuronAtom(T value, {bool equals(T a, T b)?, T guard(T current, T next)?, VoidCallback? onListen, VoidCallback? onCancel})
- Creates a NeuronAtom with an initial value.
Properties
- equals → bool Function(T a, T b)?
-
Custom equality function to determine if value has changed.
final
- guard → T Function(T current, T next)?
-
Value guard/transformer called before setting a new value.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether this atom has any listeners.
no setter
- initialValue → T
-
The initial value of the atom.
no setter
- isDisposed → bool
-
Whether this atom has been disposed.
no setter
- onCancel → VoidCallback?
-
Callback invoked when the last listener unsubscribes.
final
- onListen → VoidCallback?
-
Callback invoked when the first listener subscribes.
final
- previousValue → T?
-
The previous value of the atom (before the last change).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value ↔ T
-
The current value of the atom.
getter/setter pair
Methods
-
addListener(
VoidCallback listener) → void - Adds a listener to be called when the value changes.
-
bind(
NeuronController parent) → T -
Available on T, provided by the SignalBinding extension
Registers this notifier to be disposed withparent. -
dispose(
) → void -
Disposes the atom, removing all listeners.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void - Manually notifies all listeners.
-
onActive(
) → void - Called when the first listener is added.
-
onInactive(
) → void - Called when the last listener is removed.
-
removeListener(
VoidCallback listener) → void - Removes a previously added listener.
-
reset(
) → void - Resets the atom to its initial value.
-
select<
R> (R selector(T value)) → NeuronAtom< R> - Creates a new atom that selects a part of this atom's value.
-
subscribe(
VoidCallback listener) → VoidCallback - Adds a listener and returns a callback that cancels the subscription when called.
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited