AsyncReactiveNotifier<T> class
A reactive notifier for handling asynchronous operations with built-in loading, error, and data states.
This class wraps async operations and automatically manages state transitions, making it easy to show loading indicators, handle errors, and display data.
Examples
Basic Usage
final users = AsyncReactiveNotifier<List<User>>();
// Execute an async operation
await users.execute(() => api.fetchUsers());
// Check states
if (users.isLoading) print('Loading...');
if (users.hasError) print('Error: ${users.error}');
if (users.hasData) print('Users: ${users.valueOrNull}');
With Reactive.async Widget
Reactive<List<User>>.async(
notifier: users,
loading: (context) => const Center(
child: CircularProgressIndicator(),
),
error: (context, error, stackTrace) => Center(
child: Column(
children: [
Text('Error: $error'),
ElevatedButton(
onPressed: () => users.execute(() => api.fetchUsers()),
child: Text('Retry'),
),
],
),
),
data: (context, users) => ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) => Text(users[index].name),
),
)
With Initial Data
// Start with cached data
final users = AsyncReactiveNotifier<List<User>>.withData(cachedUsers);
// Later refresh from API
users.execute(() => api.fetchUsers());
Update Existing Data
final users = AsyncReactiveNotifier<List<User>>();
await users.execute(() => api.fetchUsers());
// Add a new user without refetching
users.updateData((list) => [...list, newUser]);
// Remove a user
users.updateData((list) => list.where((u) => u.id != deletedId).toList());
Manual State Control
final users = AsyncReactiveNotifier<List<User>>();
// Set states manually
users.setLoading();
users.setData([user1, user2]);
users.setError(Exception('Something went wrong'));
users.reset(); // Back to initial state
See also:
- ReactiveNotifier for synchronous value changes
- Reactive for building UI that responds to changes
- AsyncValue for the underlying state representation
- Inheritance
-
- Object
- ChangeNotifier
- AsyncReactiveNotifier
Constructors
- AsyncReactiveNotifier()
- Creates an AsyncReactiveNotifier with an initial state.
- AsyncReactiveNotifier.withData(T data)
- Creates an AsyncReactiveNotifier with initial data.
Properties
- error → Object?
-
Returns the error if in error state, otherwise null.
no setter
- hasData → bool
-
Whether data is available.
no setter
- hasError → bool
-
Whether the last operation completed with an error.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- isInitial → bool
-
Whether the notifier is currently in the initial state.
no setter
- isLoading → bool
-
Whether an async operation is currently in progress.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stackTrace → StackTrace?
-
Returns the stack trace if in error state, otherwise null.
no setter
-
state
→ AsyncValue<
T> -
The current async state.
no setter
- valueOrNull → T?
-
Returns the data if available, otherwise null.
no setter
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object 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
-
execute(
Future< T> asyncFunction()) → Future<void> - Executes an async function and automatically manages state transitions.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
refresh(
) → void - Forces a notification to all listeners.
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
reset(
) → void - Resets the state to initial.
-
setData(
T data) → void - Sets the state to data with the given value.
-
setError(
Object error, {StackTrace? stackTrace}) → void - Sets the state to error.
-
setLoading(
) → void - Sets the state to loading.
-
toString(
) → String -
A string representation of this object.
override
-
updateData(
T updater(T current)) → void - Updates the data if currently in data state.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited