Injected<T> class abstract

A Wrapper class that encloses the state of the model we want to Inject. The state can be mutable or immutable and can also be global or local.

Injected model can be instantiated globally or as a member of classes. They can be instantiated inside the build method without losing the state after rebuilds.

  • Injected instantiation: To instantiate an Injected model, you use RM.inject, RM.injectFuture, RM.injectStream or RM.injectFlavor.

  • Injected lifecycle: The state wrapped by the Injected model has a lifecycle. It is created when first used and destroyed when no longer used even if it is declared globally. Between the creation and the destruction of the state, it can be listened to and mutated to notify its registered listeners.

  • Injected state and null safety: The state of an injected model is null safe, that is it can not be null. For this reason the initial state will be inferred by the library, and in case it is not, it must be defined explicitly. The initial state of primitives is inferred as follows: (int: 0, double, 0.0, String:'', and bool: false). For other non-primitive objects the initial state will be the first created instance.

  • Listening to an Injected: To listen to an Injected model you can use one of the following options: ReactiveModelBuilder.listen, ReactiveModelBuilder.futureBuilder, ReactiveModelBuilder.streamBuilder, ReactiveModelBuilder.rebuilder, ReactiveModelBuilder.whenRebuilder, and ReactiveModelBuilder.whenRebuilderOr.

  • Injected state mutation: To mutate the state and notify listeners, you use ReactiveModel.state setter, ReactiveModel.setState, or ReactiveModel.toggle if the state is bool. You can also notify listeners without changing the state using ReactiveModel.notify. You can also refresh the state to its initial state and reinvoke the creation function then notify listeners using ReactiveModel.refresh.

  • Injected state cleaning: When the state is disposed of, its list of listeners is cleared, and if the state is waiting for a Future or subscribed to a Stream, it will cancel them to free resources.

  • State persistence: Injected state can be persisted using PersistState. By default the state is persisted each time the state is mutated. You can set it to persist manually or when the state is disposed. You can also throttle state persistence for the time you want.

  • Undo redo state mutation: You can undo or redo state mutation by defining the undoStackLength parameter of RM.inject RM.injectFuture, RM.injectStream or RM.injectFlavor. After state mutation you can redo it using ReactiveModelUndoRedoState.undoState and redo it using ReactiveModelUndoRedoState.redoState.

  • Injected model dependence Injected models can depend on other Injected models and recalculate its state and notify its listeners whenever any of its of the Inject model that it depends on emits a notification.

  • Injected mock for testing: Injected model can be easily mocked to fake implementation in tests using : Injected.injectMock, Injected.injectFutureMock, or Injected.injectStreamMock.

Inheritance
Implementers
Available Extensions

Constructors

Injected({required T creator(), T? initialState, SideEffects<T>? sideEffects, StateInterceptor<T>? stateInterceptor, bool autoDisposeWhenNotUsed = true, String? debugPrintWhenNotifiedPreMessage, Object? toDebugString(T?)?, int undoStackLength = 0, PersistState<T> persist()?, DependsOn<T>? dependsOn})
A Wrapper class that encloses the state of the model we want to Inject. The state can be mutable or immutable and can also be global or local.
factory
Injected.future({required Future<T> creator(), T? initialState, SideEffects<T>? sideEffects, StateInterceptor<T>? stateInterceptor, bool autoDisposeWhenNotUsed = true, String? debugPrintWhenNotifiedPreMessage, Object? toDebugString(T?)?, int undoStackLength = 0, PersistState<T> persist()?, DependsOn<T>? dependsOn, bool isLazy = false})
Create an Injected state from future
factory
Injected.generic({required Object? creator(), required T? initialState, required SideEffects<T>? sideEffects, required StateInterceptor<T>? stateInterceptor, required bool autoDisposeWhenNotUsed, required String? debugPrintWhenNotifiedPreMessage, required Object? toDebugString(T?)?, required int undoStackLength, required PersistState<T> persist()?, required DependsOn<T>? dependsOn, Object? watch(T? s)?})
Create an Injected state from any kind of creator
factory
Injected.stream({required Stream<T> creator(), T? initialState, SideEffects<T>? sideEffects, StateInterceptor<T>? stateInterceptor, bool autoDisposeWhenNotUsed = true, String? debugPrintWhenNotifiedPreMessage, Object? toDebugString(T?)?, int undoStackLength = 0, PersistState<T> persist()?, DependsOn<T>? dependsOn, Object? watch(T? s)?})
Create an Injected state from stream
factory

Properties

canRedoState bool
Whether the state can be redone.
no setter
canUndoState bool
Whether the state can be done
no setter
connectionState ConnectionState
no setterinherited
customStatus Object?
Custom status of the state. Set manually to mark the state with a particular tag to be used in your logic.
getter/setter pairinherited
error → dynamic
The error
no setterinherited
hasData bool
The state is mutated successfully.
no setterinherited
hasError bool
The stats has error
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hasObservers bool
Whether the state has listeners or not
no setterinherited
isDone bool
The state is mutated using a stream and the stream is done.
no setterinherited
isIdle bool
The state is initialized and never mutated.
no setterinherited
isStateInitialized bool
no setterinherited
isWaiting bool
The state is waiting for and asynchronous task to end.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
snapState SnapState<T>
A snap representation of the state
no setterinherited
state ↔ T
The current state
getter/setter pairinherited
stateAsync Future<T>
The current Async state
getter/setter pairinherited
subscription StreamSubscription?
It is not null if the state is waiting for a Future or is subscribed to a Stream
getter/setter pairinherited

Methods

addCleaner(VoidCallback listener) VoidCallback
Add a callback to be executed when the state is disposed of.
inherited
addObserver({required ObserveReactiveModel listener, bool shouldAutoClean = false, bool isSideEffects = true}) VoidCallback
Add observer to this state.
inherited
call(BuildContext context, {bool defaultToGlobal = false}) Injected<T>
Obtain the Injected model from the nearest InheritedWidget inserted using inherited.
cleanState() → void
Clean the state
inherited
clearUndoStack() → void
Clear undoStack;
deletePersistState() → void
Delete the state form the persistence store
dispose() → void
Dispose the state
inherited
disposeIfNotUsed() → void
Dispose the state if it has no listener
inherited
inherited({Key? key, required Widget builder(BuildContext), required FutureOr<T> stateOverride()?, bool connectWithGlobal = true, SideEffects<T>? sideEffects, String? debugPrintWhenNotifiedPreMessage, String toDebugString(T?)?}) Widget
Provide the injected model using an InheritedWidget that wraps its state.
initializeState() FutureOr<T?>
Initialize the state
inherited
injectFutureMock(Future<T> fakeCreator()) → void
Inject a fake future implementation of this injected model.
injectMock(T fakeCreator()) → void
Inject a fake implementation of this injected model.
injectStreamMock(Stream<T> fakeCreator()) → void
Inject a fake stream implementation of this injected model.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify() → void
Notify observers
inherited
of(BuildContext context, {bool defaultToGlobal = false}) → T
Obtain the state from the nearest InheritedWidget inserted using inherited.
onAll<R>({R onIdle()?, required R onWaiting()?, required R onError(dynamic error, VoidCallback refreshError)?, required R onData(T data)}) → R
Listen to the injected Model and rebuild when it emits a notification.
inherited
onOrElse<R>({R onIdle()?, R onWaiting()?, R onError(dynamic error, VoidCallback refreshError)?, R onData(T data)?, required R orElse(T data)}) → R
Listen to the injected Model and rebuild when it emits a notification.
inherited
persistState() → void
Persist the state
redoState() → void
Redo to the next valid state (isWaiting and hasError are ignored)
refresh() Future<T?>
Refresh the Injected state. Refreshing the state means reinitialize it and reinvoke its creation function and notify its listeners.
inherited
reInherited({Key? key, required BuildContext context, required Widget builder(BuildContext)}) Widget
Provide the Injected model to another widget tree branch.
setState(Object? mutator(T s), {SideEffects<T>? sideEffects, StateInterceptor<T>? stateInterceptor, bool shouldOverrideDefaultSideEffects(SnapState<T> snap)?, int debounceDelay = 0, int throttleDelay = 0}) Future<T?>
Mutate the state of the model and notify observers.
inherited
setToHasData(dynamic data) → void
Set the state to the data status
inherited
setToHasError(dynamic error, {StackTrace? stackTrace, VoidCallback? refresher}) → void
Set the state to the error status
inherited
setToIsIdle() → void
Set the state to the idle status
inherited
setToIsWaiting() → void
Set the state to the waiting status
inherited
toString() String
A string representation of this object.
inherited
undoState() → void
Undo to the last valid state (isWaiting and hasError are ignored)
whenConnectionState<R>({R onIdle()?, required R onWaiting()?, required R onError(dynamic error)?, required R onData(T data)}) → R
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited