useEffect function

void useEffect(
  1. VoidCallback? effect(), [
  2. List<Object?>? keys
])

Runs effect in the first build, and the function it returns, if any, when the element is unmounted. With keys, in a later build whose keys differ from the last one's, the function the last run returned runs first, then effect again; without, that happens in every build.

The effect runs within the build, before it goes on: a change it makes to the state of the widget building does not rebuild it again.

A subscription made when the widget first builds and cancelled when it goes, made anew for another stream:

HookBuilder(
  builder: (context) {
    final last = useRef<String?>(null);
    useEffect(() {
      final subscription = lines.listen((line) => last.value = line);
      return subscription.cancel;
    }, [lines]);
    return const Text('Listening');
  },
)

Implementation

void useEffect(VoidCallback? Function() effect, [List<Object?>? keys]) =>
    use(_EffectHook(effect, keys));