Skip to main content

avoid-hooks-outside-build

added in: 1.12.0

Warns when a hook is used outside the build method, other hooks or HookBuilder.

Example

❌ Bad:

class MyWidget extends HookWidget {
void myCallback() {
useMemoized(() => null); // LINT
}


Widget build(BuildContext context) {
return FloatingActionButton(onPressed: () {
useMemoized(() {}); // LINT
});
}
}

✅ Good:

class MyWidget extends HookWidget {

Widget build(BuildContext context) {
useMemoized(() {});

return FloatingActionButton(onPressed: () {
...
});
}

void _useMyHook() {
useMemoized(() => null);
}
}