ensure<T extends NeuronController> static method

T ensure<T extends NeuronController>(
  1. T factory()
)

Returns an existing controller of type T if present.

If the controller doesn't exist, it creates it via factory, installs it, and returns it. This is the recommended way to access controllers in Neuron.

The typical pattern is to use ensure in a static getter:

class CounterController extends NeuronController {
  late final count = Signal<int>(0).bind(this);

  static CounterController get init =>
      Neuron.ensure<CounterController>(() => CounterController());
}

Then in your UI:

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final c = CounterController.init; // Lazily created on first access
    return Slot<int>(
      connect: c.count,
      to: (ctx, val) => Text('$val'),
    );
  }
}

Returns the controller instance (existing or newly created).

See also:

  • use - Get an already installed controller (throws if not found)
  • install - Manually register a controller

Implementation

static T ensure<T extends NeuronController>(T Function() factory) {
  if (_registry.containsKey(T)) {
    return _registry[T] as T;
  }
  return install<T>(factory());
}