RtProvider<T extends Object?> class

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

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

class OtherWidget extends StatelessWidget {
  ...
  @override
  Widget build(context) {
    // Get the dependency 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` dependency.
            context.watch<MyController>((inst) => [inst.stateB]);

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

NOTE: RtProvider is a "scoped". This mean that RtProvider exposes the instance of T dependency to 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 dependency. e.g.:

...
RtProvider<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:

RtProvider<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 of T dependency after mounting.

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

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

RtProvider.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

RtProvider(InstanceBuilder<T> instanceBuilder, {Key? key, String? id, DependencyMode mode = DependencyMode.builder, Widget? child, InstanceChildBuilder<T>? builder})
Creates an instance of T dependency and provides it to tree widget.
const
RtProvider.init(InstanceBuilder<T> instanceBuilder, {Key? key, String? id, DependencyMode mode = DependencyMode.builder, Widget? child, InstanceChildBuilder<T>? builder})
RtProvider.lazy(InstanceBuilder<T> instanceBuilder, {Key? key, String? id, DependencyMode mode = DependencyMode.builder, Widget? child, ChildBuilder? builder})
Creates a lazy instance of T dependency 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 dependency 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 dependency 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 → DependencyMode
It's used to specify the type of dependency 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() → RtProviderElement<T>
Inflates this configuration to a concrete instance.
override
createInstance() → T?
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
dispose() → void
disposeInstance() → void
findInstance() → T?
getInstance() → T?
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerInstance() → void
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, int wrapWidth = 65}) → 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 dependency and sets the BuildContext to listen for when it should be re-rendered.