LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

SignalProvider

A premium dependency-injection / state propagation widget that allows passing.

A premium dependency-injection / state propagation widget that allows passing reactive signals down the Flutter widget tree using InheritedNotifier.

SignalProvider makes a signal accessible to all child widgets in the subtree. Any child widget that reads the signal using SignalProvider.of(context) will automatically rebuild when the signal's value changes, while parent widgets remain unaffected.

For version 7, SignalProvider is a stateful widget that manages the lifecycle of the created signal, ensuring it is persisted across parent rebuilds and automatically calling dispose() when the provider is unmounted to prevent memory leaks.

Example Usage#

1. Standard Constructor (Manages Lifecycle)

SignalProvider<CounterSignal>(
  create: () => CounterSignal(0),
  child: const CounterDisplay(),
)

2. Value Constructor (Exposes Existing Instance)

If the signal is created elsewhere (e.g. in a StatefulWidget's State or globally) and you want to expose it without managing its lifecycle or calling dispose, use SignalProvider.value:

SignalProvider<CounterSignal>.value(
  value: myCounterSignal,
  child: const CounterDisplay(),
)

3. Multi-Providing Multiple Signals

Wrap multiple providers in a flat list to avoid deeply nested trees using SignalProvider.multi:

SignalProvider.multi(
  providers: [
    SignalProvider<Counter>(create: () => Counter()),
    SignalProvider<User>(create: () => User()),
  ],
  child: const MyApp(),
)

Constructors#

View Constructors
SignalProvider({super.key, required T Function() create, this.child, this.dispose})

Creates a SignalProvider that manages the lifecycle of a created signal.

The create callback is invoked once to instantiate the signal. When this provider is unmounted, it automatically calls dispose() on the signal.

SignalProvider.value({super.key, required T value, this.child})

Exposes an existing signal value to the widget tree.

Unlike the default constructor, the signal is NOT created by this provider, and its lifecycle (including disposal) must be managed elsewhere.

SignalProvider._({super.key, required T Function()? create, required T? value, required this.child, required this.dispose})

Private constructor for internal cloning and subclass usage.

SignalProvider.multi({Key? key, required List<SignalProvider> providers, required Widget child})

Creates a SignalProvider that wraps multiple other SignalProviders.

This is a convenience constructor to avoid deeply nested trees when providing multiple signals.

Properties#

View Properties
Widget? child

The widget subtree that will have access to the signal.

void Function(T)? dispose

An optional custom dispose callback.

Methods#

View Methods
SignalProvider<T> copyWith(Widget child)

Returns a clone of this SignalProvider with a new child widget. Used internally by MultiSignalProvider.

State<SignalProvider<T>> createState()
static SignalProvider<T>? providerOf(BuildContext context, {bool listen = true})

Retrieves the SignalProvider widget itself from the ancestor path.

Note: Prefer using SignalProvider.of(context) to retrieve the reactive signal directly.

static T? of(BuildContext context, {bool listen = true})

Retrieves the reactive signal instance of type T directly from the nearest SignalProvider.

  • If listen is true (default), the calling widget will automatically subscribe to the signal and rebuild whenever the signal's value changes.
  • If listen is false, the signal is returned without establishing a subscription. Use listen: false when mutating the signal inside action callbacks.