AsyncNotifier<S> class
abstract
A concrete implementation of BaseNotifier
for managing asynchronous state using AsyncState.
AsyncNotifier simplifies handling loading, data, and error states for async operations such as network requests, database queries, or any operation that can be in a loading, success, or error state.
S
is the type of data managed by the notifier.
This class integrates with AsyncNotifierLifecycle
for automatic initialization
and provides convenient methods for transitioning between loading, data, and error states.
Example usage:
class MyAsyncNotifier extends AsyncNotifier<String> {
@override
Future<AsyncState<String>> onInit() async {
// Simulate network request
await Future.delayed(Duration(seconds: 2));
return const AsyncState.data('Hello from AsyncNotifier!');
}
}
Constructors
- AsyncNotifier.new()
- Creates an AsyncNotifier that starts in the loading state.
- AsyncNotifier.withData(S data)
- Creates an AsyncNotifier with initial data.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
state
→ BaseAsyncState<
S> -
Returns the current state.
no setterinherited
Methods
-
addListener(
VoidCallback listener) → void -
Adds a listener to be notified when the state changes.
inherited
-
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener will throw after the object is disposed).
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → Never -
Overrides
notifyListeners
to prevent direct calls.inherited -
onInit(
) → Future< AsyncState< S> > -
Called when the notifier is first initialized and attached to the widget tree.
inherited
-
onUpdate(
BaseAsyncState< S> state) → void -
Called when the notifier's async state is updated.
inherited
-
removeListener(
VoidCallback listener) → void -
Removes a previously added listener.
inherited
-
setData(
S data, {bool forced = false, bool notify = true}) → void -
Transitions the notifier to the data state with the given
data
. -
setError(
Object error, {String? message, StackTrace? stackTrace}) → void - Transitions the notifier to the error state with the given error information.
-
setLoading(
) → void - Transitions the notifier to the loading state.
-
setState(
BaseAsyncState< S> next, {bool notify = true, bool forced = false, bool equalityCheck(S? curr, S? next)?}) → void -
Updates the state with the given
next
state. -
stateEqualityCheck(
BaseAsyncState< S> curr, BaseAsyncState<S> next) → bool - Custom equality check for async states.
-
toString(
) → String -
Returns a string representation of the notifier.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited