LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

Type: useSignal

API reference and details for useSignal from signals.dart.

useSignal#

Kind: function  |  Package: package:signals_hooks

Function: useSignal#

FlutterSignal<T> useSignal(T value, {List<Object?> keys = const <Object>[], SignalOptions<T>? options})

Creates a new Signal that persists across widget rebuilds and subscribes to it.

The signal is instantiated once using useMemoized and automatically disposed of or cleaned up if necessary (signals created in this manner are managed by the Hook lifetime). The widget will automatically rebuild whenever this signal's value changes.

If you want the signal to survive widget disposal (e.g. shared state), do not use useSignal. Instead, define your signal globally or in a controller, and subscribe to it using useExistingSignal or useSignalValue.

Parameters#

  • value: The initial value of the signal.
  • keys: A list of objects to watch for changes. If any key in this list changes, the signal is re-created with the current value as its initial value.
  • debugLabel: An optional debug label to identify the signal in developer tools.

Returns#

A local FlutterSignal instance representing the reactive state.

Example#

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:signals_hooks/signals_hooks.dart';

class CounterHookWidget extends HookWidget {
  const CounterHookWidget({super.key});

  @override
  Widget build(BuildContext context) {
    // Create a local signal managed by this HookWidget's lifecycle
    final count = useSignal(0, keys: const [], options: SignalOptions(name: 'localCounter'));

    return Scaffold(
      body: Center(
        child: Text(
          'Count: ${count.value}',
          style: Theme.of(context).textTheme.headlineMedium,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => count.value++,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

References#

The useSignal type is referenced and used in the following pages: