Skip to main content

prefer-correct-handler-name

added in: 1.11.0
⚙️

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.

Set ignore-functions (default is true) to exclude global function used as tear-offs.

Set ignore-public (default is false) to exclude public function or method invocations from the check.

dart_code_metrics:
...
rules:
...
- prefer-correct-handler-name:
name-pattern: ^handle[A-Z]+
ignore-functions: true
ignore-public: false

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() {}
}