Skip to content

prefer_use_prefix

v0.4.0 Warning Fix Hook Rules

Flags functions and methods that call hooks internally but do not follow the use prefix naming convention. Custom hooks must start with use (or _use for private functions) so that the hooks framework and other lint rules can identify them as hooks.

The use prefix is a critical convention in the hooks ecosystem. Without it, lint rules like avoid_conditional_hooks cannot detect that a function is a hook, leading to missed warnings. Consistent naming also helps developers immediately recognize hook functions in code review.

See also: flutter_hooks - Custom hooks

// Top-level function calling hooks without 'use' prefix
String myCustomHook() {
return useMemoized(() => 'hello');
}
// Private function without '_use' prefix
int _myPrivateHook() {
return useState(0);
}
class BadWidget extends HookWidget {
int _fetchData() {
return useState(42);
}
@override
Widget build(BuildContext context) {
final data = _fetchData();
return Text('$data');
}
}
// Top-level function with 'use' prefix
String useCustomHook() {
return useMemoized(() => 'hello');
}
// Private function with '_use' prefix
int _usePrivateHook() {
return useState(0);
}
class GoodWidget extends HookWidget {
int _useData() {
return useState(42);
}
@override
Widget build(BuildContext context) {
final data = _useData();
return Text('$data');
}
}
// Regular functions that don't call hooks need no prefix:
int regularFunction() => 42;

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_use_prefix: false