useState<T> function Null safety

ValueNotifier<T> useState<T>(
  1. T initialData

Creates a variable and subscribes to it.

Whenever ValueNotifier.value updates, it will mark the caller HookWidget as needing build. On first call, inits ValueNotifier to initialData. initialData is ignored on subsequent calls.

The following example showcase a basic counter application.

class Counter extends HookWidget {
  Widget build(BuildContext context) {
    final counter = useState(0);

    return GestureDetector(
      // automatically triggers a rebuild of Counter widget
      onTap: () => counter.value++,
      child: Text(counter.value.toString()),

See also:


ValueNotifier<T> useState<T>(T initialData) {
  return use(_StateHook(initialData: initialData));