prefer_correct_callback_field_name
v1.0.0 Warning Class Naming
This rule flags a callback field or parameter named somethingCallback, somethingHandler, somethingListener or somethingAction rather than onSomething.
Why use this rule
Section titled “Why use this rule”onTap, onChanged and onPressed run through the whole Flutter API, so on... is what a reader recognises as “this fires when something happens”. A field called tapCallback carries the same meaning in a spelling every codebase invents differently, and at the call site MyWidget(tapCallback: ...) reads as a value where onTap: reads as an event.
This rule is in the pedantic preset, because naming conventions are a house style rather than a correctness question.
See also: Effective Dart: naming
What is never reported
Section titled “What is never reported”- A function named for what it computes.
builder,comparatorandparsesay what they produce, not when they fire; renaming any of them toon...would be wrong. Only a name that positively ends in a callback word is considered. - A bare framework noun. A parameter named exactly
handler,listeneroractionis the thing itself rather than a callback for an event —Handler middleware(Handler handler)in dart_frog is the request handler, andonHandlerwould be nonsense. The suffix has to follow something. - An
@override, whose name belongs to the base declaration, and a field-initialising parameter (this.onTap), which takes its name from the field the rule already checks.
class ConfirmButton extends StatelessWidget { const ConfirmButton({super.key, required this.tapCallback});
final void Function() tapCallback;}class ConfirmButton extends StatelessWidget { const ConfirmButton({super.key, required this.onTap});
final void Function() onTap;}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_callback_field_name: enabled: trueTurning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_correct_callback_field_name: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_correct_error_name— Name exception and error classes with the matching suffix.prefer_correct_handler_name— Name event handlers after the event they answer.prefer_correct_setter_parameter_name— Use one parameter name in every setter.prefer_boolean_prefixes— Name booleans as questions.