prefer-correct-handler-name
Warns when the callback handler name does not matched the configured one.
note
This rule does not take _
into account when checking the name.
⚙️ Config
Set name-pattern
(default is ^handle[A-Z]+
) to set the expected field name.
dart_code_metrics:
...
rules:
...
- prefer-correct-handler-name:
name-pattern: ^handle[A-Z]+
Example
❌ Bad:
class MyHomePage extends StatefulWidget {
final void Function() onWork;
const MyHomePage(this.onWork);
_MyHomePageState createState() => _MyHomePageState<T>();
}
class _MyHomePageState<T> extends State<MyHomePage> {
Widget build(BuildContext context) {
SomeClassWithCallbacks(
someWork, // LINT
onSecond: someWork, // LINT
onThird: () => someWork(), // LINT
onForth: () async => await someAsyncWork(), // LINT
);
}
void someWork() {}
Future<void> someAsyncWork() {}
void _handleWork() {}
}
✅ Good:
class MyHomePage extends StatefulWidget {
final void Function() onWork;
const MyHomePage(this.onWork);
_MyHomePageState createState() => _MyHomePageState<T>();
}
class _MyHomePageState<T> extends State<MyHomePage> {
Widget build(BuildContext context) {
SomeClassWithCallbacks(
_handleWork,
onSecond: _handleWork,
onThird: widget.onWork,
onForth: _handleWork,
);
}
void someWork() {}
Future<void> someAsyncWork() {}
void _handleWork() {}
}