NodeProvider<N extends NodeInterface>.reuse constructor

NodeProvider<N extends NodeInterface>.reuse({
  1. Key? key,
  2. required N create(),
  3. required Widget builder(
    1. BuildContext context,
    2. N node
    )?,
})

If the node you want to use might be created in a parent scope but you don't want to recreate it,use this constructor.

If node does not exist, it will be created.

❌ Don't do this:

final yourNode = YourNode();
NodeProvider.reuse(
 create: () => yourNode,
 builder: (context, node) => MyWidget(node: yourNode),
);

The node returned by the builder might not be the one you created.

✅ Do this:

NodeProvider.reuse(
 create: () => YourNode(),
 builder: (context, node) => MyWidget(node: node),
);

Implementation

NodeProvider.reuse({
  super.key,
  required N Function() create,
  required this.builder,
}) : child = null,
     nodes = [create],
     reuse = true;