Skip to content

prefer_correct_handler_name

v1.0.0WarningConfigurableClass Naming

This rule flags a method passed as an event handler that is not named _onSomething or _handleSomething.

A handler is the other half of the onTap:/onChanged: convention that prefer_correct_callback_field_name enforces on the parameter. When the parameter is onTap: and the method behind it is submit, the call site reads onTap: submit and the reader holds the mapping themselves; onTap: _onTap states it once.

Only a tear-off passed to an on... parameter is considered. A closure is not a named handler, a method called normally is not a handler at all, and a parameter that is not named on... gives the rule no anchor to judge by.

The prefix must start a new word, so _online does not satisfy on.

This rule is in the pedantic preset.

Button(onTap: _submit);
Button(onTap: _onTap);
Button(onTap: _handleTap);

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
prefer_correct_handler_name:
enabled: true
analysis_options.yaml
many_lints:
rules:
prefer_correct_handler_name:
prefixes: [on, handle]
additional_prefixes: [process]
require_private: true
Option Type Default Description
prefixes list [on, handle] Accepted handler prefixes
additional_prefixes list [] Prefixes to add without replacing the defaults
require_private bool true Whether a handler must also start with an underscore

To disable this rule:

many_lints.yaml
rules:
prefer_correct_handler_name: false

To keep the rule on but skip certain paths, use per-rule exclude.