Store<St> class
Creates a Redux store that holds the app state.
The only way to change the state in the store is to dispatch a ReduxAction. You may implement these methods:
-
AppState reduce()
➜ To run synchronously, just return the state: AppState reduce() { ... return state; } To run asynchronously, return a future of the state: Future -
FutureOr<void> before()
➜ Runs before the reduce method. If it throws an error, thenreduce
will NOT run. To runbefore
synchronously, just return void: void before() { ... } To run asynchronously, return a future of void: Future -
void after()
➜ Runs afterreduce
, even if an error was thrown bybefore
orreduce
(akin to a "finally" block). If theafter
method itself throws an error, this error will be "swallowed" and ignored. Avoidafter
methods which can throw errors. -
bool abortDispatch()
➜ If this returns true, the action will not be dispatched:before
,reduce
andafter
will not be called. This is only useful under rare circumstances, and you should only use it if you know what you are doing. -
Object? wrapError(error)
➜ If any error is thrown bybefore
orreduce
, you have the chance to further process it by usingwrapError
. Usually this is used to wrap the error inside of another that better describes the failed action. For example, if some action converts a String into a number, then instead of throwing a FormatException you could do:wrapError(error) => UserException("Please enter a valid number.", cause: error)
• ActionObserver observes the dispatching of actions, and may be used to print or log the dispatching of actions.
• StateObservers receive the action, prevState (state right before the new State is applied), newState (state that was applied), and are used to track metrics and more.
• ErrorObservers may be used to observe or process errors thrown by actions.
• GlobalWrapError may be used to wrap action errors globally.
For more info, see: https://pub.dartlang.org/packages/async_redux
- Implementers
Constructors
-
Store({required St initialState, Object? environment, Map<
Object?, Object?> props = const {}, bool syncStream = false, TestInfoPrinter? testInfoPrinter, List<ActionObserver< ? actionObservers, List<St> >StateObserver< ? stateObservers, Persistor<St> >St> ? persistor, ModelObserver? modelObserver, ErrorObserver<St> ? errorObserver, WrapReduce<St> ? wrapReduce, @Deprecated("Use `globalWrapError` instead. This will be removed.") WrapError<St> ? wrapError, GlobalWrapError<St> ? globalWrapError, bool? defaultDistinct, CompareBy? immutableCollectionEquality, int? maxErrorsQueued})
Properties
- defaultDistinct → bool
-
no setter
- dispatchCount → int
-
no setter
- env → Object?
-
Gets the store environment.
This can be used to create a global value, but scoped to the store.
For example, you could have a service locator, here, or a configuration value.
no setter
-
errors
→ Queue<
UserException> -
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- immutableCollectionEquality → CompareBy?
-
If
null
(the default), view-models which are immutable collections will be compared by their default equality.If
CompareBy.byDeepEquals
, view-models which are immutable collections will be compared by their items, one by one (potentially slow comparison).If
CompareBy.byIdentity
, view-models which are immutable collections will be compared by their internals being identical (very fast comparison).no setter - isShutdown → bool
-
no setter
- modelObserver → ModelObserver?
-
no setter
-
onChange
→ Stream<
St> -
A stream that emits the current state when it changes.
no setter
-
onReduce
→ Stream<
TestInfo< St> > -
Used by the storeTester.
no setter
- reduceCount → int
-
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → St
-
The current state of the app.
no setter
- stateTimestamp → DateTime
-
The timestamp of the current state in the store, in UTC.
no setter
- testInfoPrinter → TestInfoPrinter?
-
no setter
Methods
-
actionsInProgress(
) → Set< ReduxAction< St> > - Returns an unmodifiable set of the actions on progress.
-
clearExceptionFor(
Object actionTypeOrList) → void -
Removes the given
actionTypeOrList
from the list of action types that failed. -
createTestInfoSnapshot(
St state, ReduxAction< St> action, Object? error, Object? processedError, {required bool ini}) → void -
defineState(
St state) → void - Beware: Changes the state directly. Use only for TESTS. This will not notify the listeners nor complete wait conditions.
-
deleteStateFromPersistence(
) → Future< void> - Asks the Persistor to delete the saved state from the local persistence.
-
dispatch(
ReduxAction< St> action, {bool notify = true}) → FutureOr<ActionStatus> - Dispatches the action, applying its reducer, and possibly changing the store state. The action may be sync or async.
-
dispatchAll(
List< ReduxAction< actions, {bool notify = true}) → List<St> >ReduxAction< St> > -
Dispatches all given
actions
in parallel, applying their reducer, and possibly changing the store state. It returns the same list ofactions
, so that you can instantiate them inline, but still get a list of them. -
dispatchAndWait(
ReduxAction< St> action, {bool notify = true}) → Future<ActionStatus> - Dispatches the action, applying its reducer, and possibly changing the store state. The action may be sync or async. In both cases, it returns a Future that resolves when the action finishes.
-
dispatchAndWaitAll(
List< ReduxAction< actions, {bool notify = true}) → Future<St> >List< ReduxAction< >St> > -
Dispatches all given
actions
in parallel, applying their reducers, and possibly changing the store state. The actions may be sync or async. It returns a Future that resolves when ALL actions finish. -
dispatchAsync(
ReduxAction< St> action, {bool notify = true}) → Future<ActionStatus> -
dispatchSync(
ReduxAction< St> action, {bool notify = true}) → ActionStatus - Dispatches the action, applying its reducer, and possibly changing the store state. However, if the action is ASYNC, it will throw a StoreException.
-
disposeProps(
[bool predicate({Object? key, Object? value})?]) → void - The disposeProps method is used to clean up resources associated with the store's properties, by stopping, closing, ignoring and removing timers, streams, sinks, and futures that are saved as properties in the store.
-
exceptionFor(
Object actionTypeOrList) → UserException? -
Returns the UserException of the
actionTypeOrList
that failed. -
getAndRemoveFirstError(
) → UserException? - Gets the first error from the error queue, and removes it from the queue.
-
getLastPersistedStateFromPersistor(
) → St? - Gets, from the Persistor, the last state that was saved to the local persistence.
-
initTestInfoController(
) → void - Turns on testing capabilities, if not already.
-
initTestInfoPrinter(
TestInfoPrinter testInfoPrinter) → void - Changes the testInfoPrinter.
-
isFailed(
Object actionOrActionTypeOrList) → bool -
Returns true if an
actionOrActionTypeOrList
failed with an UserException. Note: This method uses the EXACT type inactionOrActionTypeOrList
. Subtypes are not considered. -
isWaiting(
Object actionOrActionTypeOrList) → bool -
You can use isWaiting and pass it
actionOrActionTypeOrList
to check if: -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pausePersistor(
) → void - Pause the Persistor temporarily.
-
persistAndPausePersistor(
) → void - Persists the current state (if it's not yet persisted), then pauses the Persistor temporarily.
-
prop<
V> (Object? key) → V - Gets a property from the store. This can be used to save global values, but scoped to the store. For example, you could save timers, streams or futures used by actions.
-
readStateFromPersistence(
) → Future< St?> - Asks the Persistor to read the state from the local persistence. Important: If you use this, you MUST put this state into the store. The Persistor will assume that's the case, and will not work properly otherwise.
-
resumePersistor(
) → void - Resumes persistence by the Persistor, after calling pausePersistor or persistAndPausePersistor.
-
saveInitialStateInPersistence(
St initialState) → Future< void> -
Asks the Persistor to save the
initialState
in the local persistence. -
setProp(
Object? key, Object? value) → void - Sets a property in the store. This can be used to save global values, but scoped to the store. For example, you could save timers, streams or futures used by actions.
-
shutdown(
) → void - Call this method to shut down the store. It won't accept dispatches or change the state anymore.
-
teardown(
{St? emptyState}) → Future - Closes down the store so it will no longer be operational. Only use this if you want to destroy the Store while your app is running. Do not use this method as a way to stop listening to onChange state changes. For that purpose, view the onChange documentation.
-
toString(
) → String -
A string representation of this object.
inherited
-
waitActionCondition(
bool condition(Set< ReduxAction< actions, ReduxAction<St> >St> ? triggerAction), {bool completeImmediately = false, String completedErrorMessage = "Awaited action condition was already true", int? timeoutMillis}) → Future<(Set< ReduxAction< , ReduxAction<St> >St> ?)> -
Returns a future that completes when some actions meet the given
condition
. -
waitActionType(
Type actionType, {bool completeImmediately = false, int? timeoutMillis}) → Future< ReduxAction< St> ?> - Returns a future that completes when an action of the given type in NOT in progress (it's not being dispatched):
-
waitAllActions(
List< ReduxAction< ? actions, {bool completeImmediately = false, int? timeoutMillis}) → Future<St> >void> -
Returns a future that completes when ALL given
actions
finish dispatching. -
waitAllActionTypes(
List< Type> actionTypes, {bool completeImmediately = false, int? timeoutMillis}) → Future<void> - Returns a future that completes when ALL actions of the given type are NOT in progress (none of them is being dispatched):
-
waitAnyActionTypeFinishes(
List< Type> actionTypes, {int? timeoutMillis}) → Future<ReduxAction< St> > - Returns a future which will complete when ANY action of the given types FINISHES dispatching. IMPORTANT: This method is different from the other similar methods, because it does NOT complete immediately if no action of the given types is in progress. Instead, it waits until an action of the given types finishes dispatching, even if they were not yet in progress when the method was called.
-
waitCondition(
bool condition(St), {bool completeImmediately = true, int? timeoutMillis}) → Future< ReduxAction< St> ?> -
Returns a future which will complete when the given state
condition
is true.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- defaultTimeoutMillis ↔ int
-
The global default timeout for the wait functions like waitCondition etc
is 10 minutes. This value is not final and can be modified.
To disable the timeout, make it -1.
getter/setter pair