ReactterProvider<T extends Object?> class

A Widget that serves as a conduit for injecting an instance of T type into the widget tree. e.g.:

class MyApp extends StatelessWidget {
  ...
  @override
  Widget build(context) {
    return ReactterProvider<MyController>(
      () => MyController(),
      builder: (context, myController, child) {
        return OtherWidget();
      },
    );
  }
}

class OtherWidget extends StatelessWidget {
  ...
  @override
  Widget build(context) {
    // Get the instance of `MyController` using the context.
    final myController = context.use<MyController>();

    return Column(
      children: [
        Text("StateA: ${myController.stateA.value}"),
        Builder(
          builder: (context){
            // Watch the `stateB` of `MyController` instance.
            context.watch<MyController>((inst) => [inst.stateB]);

            return Text("StateB: ${myController.stateB.value}");
          },
        ),
      ],
    );
  }
}

NOTE: ReactterProvider is a "scoped". This mean that ReactterProvider exposes the instance of T type defined on second parameter(InstanceChildBuilder) through the BuildContext in the widget subtree:

In the above example, stateA remains static while the Builder is rebuilt according to the changes in stateB. Because the Builder's context kept in watch of stateB.

RECOMMENDED: Dont's use Object with constructor parameters to prevent conflicts.

Use id property to identify the T instance. e.g.:

...
ReactterProvider<MyController>(
  () => MyController(),
  id: 'uniqueId,
  builder: (context, myController, child) {
    return OtherWidget();
  },
);
...
final myController = context.use<MyController>(id: 'uniqueId');
...

Use child property to pass a Widget which to be built once only. It will be sent through the builder callback, so you can incorporate it into your build:

ReactterProvider<MyController>(
  () => MyController(),
  child: Text("This widget build only once"),
  builder: (context, _, __) {
    final myController = context.watch<MyController>();

    return Column(
      children: [
        Text("state: ${myController.stateA.value}"),
        child,
      ],
    );
  },
)

Use init property as true to create the instance after mounting.

NOTE: The init property is false by default. This means that the instance will be created in the mounting.

Use ReactterProvider.lazy contructor for creating a lazy instance. This is particularly useful for optimizing performance by lazy-loading instance only when it is needed. e.g.:

ReactterProvider.lazy(
  () => MyController(),
  child: Text('Do Something'),
  builder: (context, child) {
    return ElevatedButton(
      onPressed: () {
        // The `MyController` instance is created here.
        context.use<MyController>.doSomething();
      },
      child: child,
    );
  },
),

See also:

Inheritance
Implemented types

Constructors

ReactterProvider(InstanceBuilder<T> instanceBuilder, {Key? key, String? id, InstanceManageMode mode = InstanceManageMode.builder, bool init = false, Widget? child, InstanceChildBuilder<T>? builder})
Creates a instance of T type and provides it to tree widget.
const
ReactterProvider.lazy(InstanceBuilder<T> instanceBuilder, {Key? key, String? id, InstanceManageMode mode = InstanceManageMode.builder, Widget? child, ChildBuilder? builder})
Creates a lazy instance of T type and provides it to tree widget.
const

Properties

builder InstanceChildBuilder<T>?
Method which has the render logic
finalinherited
child Widget?
The child widget that will be wrapped by the provider. The child widget can be accessed within the builder method of the provider.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
id String?
It's used to identify the instance of T type that is provided by the provider.
finalinherited
init bool
The child widget that will be wrapped by the provider. The child widget can be accessed within the builder method of the provider.
finalinherited
instanceBuilder InstanceBuilder<T>
Create a T instance.
finalinherited
isLazy bool
Lazily create the instance defined on firts parameter(instanceBuilder).
finalinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
lazyBuilder ChildBuilder?
Method which has the render logic
finalinherited
mode InstanceManageMode
It's used to specify the type of instance creation for the provided object.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

buildWithChild(Widget? child) Widget
createElement() ReactterProviderElement<T>
Inflates this configuration to a concrete instance.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

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

Static Methods

contextOf<T extends Object?>(BuildContext context, {String? id, ListenStates<T>? listenStates, bool listen = true}) → T
Returns an instance of T and sets the BuildContext to listen for when it should be re-rendered.