bindSignal<T, S extends ReadonlySignal<T>> function

  1. @Deprecated('use SignalsMixin > bindSignal instead')
S bindSignal<T, S extends ReadonlySignal<T>>(
  1. BuildContext context,
  2. S target, {
  3. String? debugLabel,
})

Bind an existing signal to a widget.

class State extends ... {
 final source = signal(0);
 late final count = bindSignal(context, source);

 @override
 Widget build(BuildContext context) {
   return Row(
    children: [
      IconButton(icon: Icon(Icons.remove), onPressed: () => count.value--),
      Text(count.value.toString()),
      IconButton(icon: Icon(Icons.add), onPressed: () => count.value++),
   ],
  );
 }
}

Implementation

@Deprecated('use SignalsMixin > bindSignal instead')
S bindSignal<T, S extends ReadonlySignal<T>>(
  BuildContext context,
  S target, {
  String? debugLabel,
}) {
  final ctx = context;
  if (ctx is StatefulElement) {
    final state = ctx.state;
    if (state is SignalsMixin) {
      return state.bindSignal<T, S>(target);
    } else if (target.autoDispose == true && state is SignalsAutoDisposeMixin) {
      state.cleanupCallbacks.add(target.dispose);
    }
  }
  watchSignal<T>(
    context,
    target,
    debugLabel: debugLabel,
  );
  return target;
}