avoid-hooks-outside-build
Warns when a hook is used outside the build method, other hooks or HookBuilder
.
⚙️ Config
Set additional-methods
(default is empty) to allow additional methods to call hooks (not only build
).
dart_code_metrics:
...
rules:
...
- avoid-hooks-outside-build:
additional-methods:
- customBuild
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);
}
}