Skip to main content

prefer-typedefs-for-callbacks

added in: 1.11.0
⚙️
Pro+

Warns when a Function type is declared not as a typedef.

Typedefs benefit from having documentation on the type itself and make it easier to read and find common callsites for the signature. Additionally, it helps avoid inconsistencies when only one of the repeated function types is updated

⚙️ Config

Set ignore-fields (default is false) to exclude fields from the check.

Set ignore-parameters (default is false) to exclude parameters from the check.

Set ignore-type-arguments (default is false) to exclude type arguments from the check.

Set ignore-return-types (default is false) to exclude return types from the check.

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-typedefs-for-callbacks:
ignore-fields: false
ignore-parameters: false
ignore-type-arguments: false
ignore-return-types: false

Example

❌ Bad:

class SomeWidget {
final void Function() onEnterButton; // LINT: Prefer typedefs over 'Function' types for callbacks. Try moving this type into a typedef or reusing an existing one.

const SomeWidget(
this.onEnterButton,
);

void another({
void Function()? onRemoved, // LINT: Prefer typedefs over 'Function' types for callbacks. Try moving this type into a typedef or reusing an existing one.
}) {}
}

✅ Good:

class SomeWidget {
final VoidCallback onEnterButton;

const SomeWidget(
this.onEnterButton,
);

void another({
VoidCallback? onRemoved,
}) {}
}