prefer_correct_handler_name
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);Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: prefer_correct_handler_name: enabled: trueOptions
Section titled “Options”many_lints: rules: prefer_correct_handler_name: prefixes: [on, handle] additional_prefixes: [process] require_private: truerules: 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 |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_correct_handler_name: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_correct_callback_field_name— Name callbacks onSomething, the way Flutter does.prefer_correct_error_name— Name exception and error classes with the matching suffix.prefer_correct_setter_parameter_name— Use one parameter name in every setter.prefer_boolean_prefixes— Name booleans as questions.