inherited method
Widget
inherited({
- required Widget builder(),
- Key? key,
- required FutureOr<
T> stateOverride()?, - bool? connectWithGlobal,
- SideEffects<
T> ? sideEffects, - String? debugPrintWhenNotifiedPreMessage,
- Object? toDebugString(
- T?
override
Provide the injected model using an InheritedWidget that wraps its state.
By default the InheritedWidget holds the state of the injected model,
but this can be overridden using the stateOverride
parameter.
Child widgets can obtain the wrapped state using .of(context)
or
.call(context)
methods.
-
myModel.of(context)
looks up in the widget tree to find the state ofmyModel
and register theBuildContext
to rebuild whenmyModel
is notified. -
myModel.call(context) or myModel(context)
looks up in the widget tree to find the injected modelmyModel
without registering theBuildContext
.
ex:
final counter1 = RM.inject<int>(()=> 0);
final counter2 = RM.inject<int>(()=> 0);
class MyApp extends StatelessWidget{
Widget build(context){
counter1.inherited(
builder: (context):{
return counter2.inherited(
builder: (context){
//Getting the counters state using `of` will
//resister this BuildContext
final int counter1State = counter1.of(context);
//Although both counters are of the same type we get
//the right state
final int counter2State = counter2.of(context);
//Getting the counters using the `call` method will
//not register this BuildContext
final Injected<int> counter1 = counter1(context);
final Injected<int> counter2 = counter2(context);
}
)
}
)
}
}
- Required parameters:
- builder: Callback to be rendered. It exposed the BuildContext.
- Optional parameters:
- stateOverride: CallBack to override the exposed state.
- connectWithGlobal: If state is overridden, whether to mutate the global
- debugPrintWhenNotifiedPreMessage: if not null, print an informative message when this model is notified in the debug mode.The entered message will pré-append the debug message. Useful if the type of the injected model is primitive to distinguish
Implementation
@override
Widget inherited({
required Widget Function(BuildContext) builder,
Key? key,
required FutureOr<T> Function()? stateOverride,
bool? connectWithGlobal,
SideEffects<T>? sideEffects,
String? debugPrintWhenNotifiedPreMessage,
Object? Function(T?)? toDebugString,
// bool keepAlive = false,
}) {
if (connectWithGlobal == null && _shouldConnectWithGlobal == null) {
if (!isInitialized) {
try {
final s = mockableCreator();
isInitialized = true;
if (s is T) {
_snapState = _snapState.copyWith(data: s);
} else if (s != null) {
setStateNullable(
(s) => mockableCreator(),
middleSetState: middleSetState,
stackTrace: kDebugMode ? StackTrace.current : null,
);
}
// _reactiveModelState.setStateFn(
// (_) => s,
// middleState: _middleSnap,
// onDone: (_) {},
// )();
} catch (e) {
if (e is! UnimplementedError) {
rethrow;
}
}
}
_shouldConnectWithGlobal = _snapState.data == null;
}
return _inherited(
builder: builder,
key: key,
stateOverride: stateOverride,
connectWithGlobal: connectWithGlobal ?? _shouldConnectWithGlobal!,
sideEffects: sideEffects,
toDebugString: toDebugString,
// keepAlive: keepAlive,
);
}