Skip to main content

prefer-correct-callback-field-name

added in: 1.11.0
⚙️
Pro+
preset: recommended

Warns when a field with a Function type does not match the configured name or a field with a non-Function type matches the configured name.

note

This rule does not take _ into account when checking the name.

⚙️ Config

Set name-pattern (default is ^on[A-Z]+) to set the expected field name.

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-callback-field-name:
name-pattern: ^on[A-Z]+

Example

❌ Bad:

class SomeWidget {
final String onSome; // LINT: This field matches the configured pattern: ^on[A-Z]+, but is not a 'Function' type field. Try renaming it.

final Function doWork; // LINT: This field with a 'Function' type does not match the configured pattern: ^on[A-Z]+. Try renaming it.

const SomeWidget(this.onSome, this.doWork);
}

✅ Good:

class SomeWidget {
final String value;

final Function onTap;

const SomeWidget(this.value, this.onTap);
}