avoid-misused-hooks
Warns when hooks are used in non-hook widgets.
⚙️ Config
Set ignored-widgets
(default is empty) to specify custom hook widgets that should be ignored (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-misused-hooks:
ignored-widgets:
- CustomHookWidget
Example
❌ Bad:
class SomeWidget extends StatelessWidget {
const SomeWidget();
Widget build(BuildContext context) {
useMemoized(() => {}, keys); // LINT: Avoid using hooks in non-hook widgets.
return Container();
}
}
✅ Good:
class SomeHookWidget extends HookWidget {
const SomeHookWidget();
Widget build(BuildContext context) {
useMemoized(() => {}, keys); // Correct, the widget is a 'HookWidget'
return Container();
}
}
Example with "ignored-widgets"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-misused-hooks:
ignored-widgets:
- CustomHookWidget
✅ Good:
abstract class CustomHookWidget extends StatelessWidget {
Widget buildWithHooks(BuildContext context, Widget? child);
Widget buildWithChild(BuildContext context, Widget? child) =>
_SingleChildHookBuilder(
builder: buildWithHooks,
child: child,
);
Widget build(BuildContext context) => buildWithChild(context, _child);
}
class MyWidget extends CustomHookWidget {
Widget buildWithHooks() {
// Correct, CustomHookWidget is ignored
useMemoized(() => {}, []);
}
}