AsyncSignal<T> class
============================================================================
ASYNC SIGNAL<T> - ASYNC STATE MANAGEMENT
Async state wrapper for handling loading, data, and error states.
AsyncSignal simplifies async operations by managing three states:
- loading: Operation in progress (isLoading == true)
- data: Operation completed successfully (hasData == true)
- error: Operation failed (hasError == true)
Uses the pure Dart AsyncState sealed class, allowing business logic to remain framework-agnostic.
Basic Usage
class UserController extends NeuronController {
late final user = AsyncSignal<User>(null).bind(this);
Future<void> loadUser() async {
user.emitLoading();
try {
final data = await api.getUser();
user.emitData(data);
} catch (e, stack) {
user.emitError(e, stack);
}
}
}
Automatic Execution
Use execute to handle the try-catch-emit flow automatically:
Future<void> loadUser() async {
await user.execute(() => api.getUser());
}
Refresh Capability
Use refresh to re-execute the last operation:
user.refresh(); // Re-runs the last operation passed to execute()
Pattern Matching
Use Dart 3's pattern matching for exhaustive state handling:
switch (user.state) {
case AsyncLoading():
return CircularProgressIndicator();
case AsyncData(:final value):
return Text(value.name);
case AsyncError(:final error):
return Text('Error: $error');
}
Binding to UI
Use AsyncSlot to handle all three states declaratively:
AsyncSlot<User>(
connect: controller.user,
onData: (ctx, user) => Text(user.name),
onLoading: (ctx) => CircularProgressIndicator(),
onError: (ctx, err) => Text('Error: $err'),
)
See also:
- AsyncState - Pure Dart sealed class for async state
- AsyncSlot - Widget for binding async signals to UI
- execute - Helper method to manage async operations
- Inheritance
-
- Object
- NeuronAtom<
AsyncState< T> > - AsyncSignal
- Implementers
Constructors
-
AsyncSignal(T? initial, {String? debugLabel, bool equals(AsyncState<
T> a, AsyncState<T> b)?, AsyncState<T> guard(AsyncState<T> current, AsyncState<T> next)?, VoidCallback? onListen, VoidCallback? onCancel}) - Creates an async signal with optional initial data.
Properties
- canRefresh → bool
-
Whether a refresh operation is possible.
no setter
- data → T?
-
Current data (null if loading or error).
no setter
- debugLabel → String?
-
Debug label for identification in DevTools.
final
-
equals
→ bool Function(AsyncState<
T> a, AsyncState<T> b)? -
Custom equality function to determine if value has changed.
finalinherited
- error → Object?
-
Current error (null if loading or has data).
no setter
-
guard
→ AsyncState<
T> Function(AsyncState<T> current, AsyncState<T> next)? -
Value guard/transformer called before setting a new value.
finalinherited
- hasData → bool
-
Whether has data.
no setter
- hasError → bool
-
Whether has error.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether this atom has any listeners.
no setterinherited
-
initialValue
→ AsyncState<
T> -
The initial value of the atom.
no setterinherited
- isDisposed → bool
-
Whether this atom has been disposed.
no setterinherited
- isLoading → bool
-
Whether currently loading.
no setter
- onCancel → VoidCallback?
-
Callback invoked when the last listener unsubscribes.
finalinherited
- onListen → VoidCallback?
-
Callback invoked when the first listener subscribes.
finalinherited
-
previousValue
→ AsyncState<
T> ? -
The previous value of the atom (before the last change).
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
state
→ AsyncState<
T> -
The current async state.
no setter
-
value
↔ AsyncState<
T> -
The current value of the atom.
getter/setter pairinherited
Methods
-
addListener(
VoidCallback listener) → void -
Adds a listener to be called when the value changes.
inherited
-
dispose(
) → void -
Disposes the atom, removing all listeners.
inherited
-
emitData(
T data) → void - Set data state.
-
emitError(
Object err, [StackTrace? stackTrace]) → void - Set error state.
-
emitLoading(
) → void - Set loading state.
-
execute(
Future< T> operation()) → Future<void> - Execute an async operation and handle states automatically.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Manually notifies all listeners.
inherited
-
onActive(
) → void -
Called when the first listener is added.
inherited
-
onInactive(
) → void -
Called when the last listener is removed.
inherited
-
refresh(
) → Future< void> - Re-execute the last operation.
-
removeListener(
VoidCallback listener) → void -
Removes a previously added listener.
inherited
-
reset(
) → void -
Resets the atom to its initial value.
inherited
-
select<
R> (R selector(AsyncState< T> value)) → NeuronAtom<R> -
Creates a new atom that selects a part of this atom's value.
inherited
-
subscribe(
VoidCallback listener) → VoidCallback -
Adds a listener and returns a callback that cancels the subscription when called.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited