inherited abstract method
Widget
inherited({})
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
Widget inherited({
Key? key,
required Widget Function(BuildContext) builder,
required FutureOr<T> Function()? stateOverride,
bool connectWithGlobal = true,
SideEffects<T>? sideEffects,
String? debugPrintWhenNotifiedPreMessage,
String Function(T?)? toDebugString,
// bool keepAlive = false,
});