Skip to content

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.

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

  • A function named for what it computes. builder, comparator and parse say what they produce, not when they fire; renaming any of them to on... would be wrong. Only a name that positively ends in a callback word is considered.
  • A bare framework noun. A parameter named exactly handler, listener or action is the thing itself rather than a callback for an event — Handler middleware(Handler handler) in dart_frog is the request handler, and onHandler would 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;
}

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

many_lints.yaml
rules:
prefer_correct_callback_field_name:
enabled: true

To disable this rule:

many_lints.yaml
rules:
prefer_correct_callback_field_name: false

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