AsyncViewModelImpl<T> class
abstract
Base ViewModel implementation for handling asynchronous operations with state management.
Provides a standardized way to handle loading, success, and error states for async data.
Constructor Parameters:
- loadOnInit: If true (default), automatically calls init() when the ViewModel is created
- waitForContext: If true (default false), waits for BuildContext to be available before calling init() when loadOnInit is true. The ViewModel stays in initial state until context is ready.
waitForContext Usage:
When waitForContext is true:
- The ViewModel stays in AsyncState.initial() until BuildContext becomes available
- Once context is available, init() is called automatically
- Useful for ViewModels that need MediaQuery, Theme, or other context-dependent data
Example:
class MyViewModel extends AsyncViewModelImpl<MyData> {
MyViewModel() : super(AsyncState.initial(), waitForContext: true);
@override
Future<MyData> init() async {
// This will only run after BuildContext is available
final theme = Theme.of(requireContext('theme access'));
return await loadDataBasedOnTheme(theme);
}
}
- Inheritance
-
- Object
- ChangeNotifier
- AsyncViewModelImpl
- Mixed-in types
Constructors
-
AsyncViewModelImpl(AsyncState<
T> _state, {bool loadOnInit = true, bool waitForContext = false})
Properties
- activeListenerCount → int
-
Get current listener count for debugging
no setter
- context → BuildContext?
-
Get current BuildContext if available
Automatically provided when any ReactiveBuilder is mounted
no setterinherited
- data → T?
-
Get the current data (may be null if not in success state)
no setter
- error → Object?
-
Get current error if any
no setter
- globalContext → BuildContext?
-
Get global BuildContext directly (bypassing specific ViewModel context)
This context is persistent and remains available throughout app lifecycle
Ideal for Riverpod/Provider migration where context stability is needed
no setterinherited
- hasContext → bool
-
Check if context is currently available
Useful for conditional context-dependent operations
no setterinherited
- hasData → bool
-
Check if the state contains valid data
no setter
- hasGlobalContext → bool
-
Check if global context is available
Global context is set via ReactiveNotifier.initContext()
and persists throughout the app lifecycle regardless of builder state
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasInitializedListenerExecution ↔ bool
-
hasInitializedListenerExecution
When registering the listener from an external function, you must first validate if all the loadData has already been initialized,
to avoid duplication when initializing our listener, because when we create a listener it executes the internal code.
We don't use loadOnInit, because we need a way to be sure that the entire cycle of our viewmodel has already been executed.
getter/setter pair
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- isDisposed → bool
-
Public getter to check if AsyncViewModel is disposed
Used by ReactiveNotifier to avoid circular dispose calls
no setter
- isLoading → bool
-
Check if any operation is in progress
no setter
- loadOnInit ↔ bool
-
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- stackTrace → StackTrace?
-
Get current stack trace if there's an error
no setter
- waitForContext ↔ bool
-
getter/setter pair
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
cleanState(
) → void -
dispose(
) → void -
Discards any resources used by the object.
override
-
errorState(
Object error, [StackTrace? stackTrace]) → void - Set error state
-
init(
) → Future< T> - Override this method to provide the async data loading logic
-
isEmptyData(
dynamic value) → bool -
inherited
-
listenVM(
void value(AsyncState< T> data), {bool callOnInit = false}) → Future<AsyncState< T> > - Starts listening for changes in the ViewModel's asynchronous state.
-
loadingState(
) → void -
loadNotifier(
) → Future< void> - loadNotifier Ensures the ViewModel's availability by confirming initialization has occurred.
-
logRemove<
T> ({String? typeName, required List< String> listeners, int level = 10}) → void -
inherited
-
logSetup<
T> ({String? typeName, required List< String> listeners, int level = 10}) → void -
inherited
-
match<
R> ({required R initial(), required R loading(), required R success(T data), required R empty(), required R error(Object? err, StackTrace? stackTrace)}) → R -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
onAsyncStateChanged(
AsyncState< T> previous, AsyncState<T> next) → void - Hook that executes automatically after every async state change
-
onDependenciesStateChanged(
DependencyState change) → void - Lifecycle hook called when any registered dependency's state changes.
-
onResume(
T? data) → FutureOr< void> -
Called after the ViewModel's primary initialization logic (e.g., in
init(), setupListeners, etc) has completed successfully. -
reinitializeWithContext(
) → void - Called when context becomes available for the first time Used to reinitialize ViewModels that were created without context
-
reload(
) → Future< void> - Public method to reload data
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
removeListeners(
{List< String> currentListeners = const []}) → Future<void> - removeListeners We remove the listeners registered in setupListeners to avoid memory problems.
-
requireContext(
[String? operation]) → BuildContext -
Get context with descriptive error if unavailable
Use when context is absolutely required for the operation
inherited
-
requireGlobalContext(
[String? operation]) → BuildContext -
Get global context with descriptive error if unavailable
Use when global context is absolutely required (e.g., Riverpod migration)
inherited
-
setupListeners(
{List< String> currentListeners = const []}) → Future<void> - setupListeners We register our listeners coming from the notifiers.
-
stopListeningVM(
) → void - Stops listening for changes in the AsyncViewModel.
-
stopSpecificListener(
String listenerKey) → void - Stops a specific listener by key Useful for more granular listener management
-
toString(
) → String -
A string representation of this object.
inherited
-
transformDataState(
T? transformer(T? data)) → void -
Transforms the data within the current success state using the
provided
transformerfunction and notifies listeners. -
transformDataStateSilently(
T? transformer(T? data)) → void -
Transforms the data within the current success state using the
provided
transformerfunction, without notifying listeners. -
transformState(
AsyncState< T> transformer(AsyncState<T> state)) → void -
Transforms the current entire AsyncState using the provided
transformerfunction and notifies listeners. -
transformStateSilently(
AsyncState< T> transformer(AsyncState<T> state)) → void -
Transforms the current entire AsyncState using the provided
transformerfunction, without notifying listeners. -
updateSilently(
T newState) → void -
Updates the internal state to a new success state with
newStatewithout notifying any listeners. -
updateState(
T data) → void - Update data directly
-
when<
R> ({required R initial(), required R loading(), required R success(T data), required R error(Object? err, StackTrace? stackTrace)}) → R
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited