useSetNotifier<E> function

SetNotifier<E> useSetNotifier<E>([
  1. Iterable<E> initial = const []
])

Creates a SetNotifier tied to the widget lifecycle.

The notifier is constructed once on first build, disposed on unmount, and listened to so the host widget rebuilds when the set mutates.

initial is consumed once on first build. Passing a different iterable on a later rebuild has no effect — the notifier was already constructed. To reset on a dependency change, scope the host widget under a different key so the hook re-mounts.

See also: SetNotifier, the underlying reactive set.

class FilterChips extends HookWidget {
  const FilterChips({super.key, required this.tags});

  final List<String> tags;

  @override
  Widget build(BuildContext context) {
    final selected = useSetNotifier<String>();
    return Wrap(
      children: [
        for (final tag in tags)
          FilterChip(
            label: Text(tag),
            selected: selected.contains(tag),
            onSelected: (_) => selected.invert(tag),
          ),
      ],
    );
  }
}

Implementation

SetNotifier<E> useSetNotifier<E>([Iterable<E> initial = const []]) {
  return use(_SetNotifierHook<E>(initial));
}