Skip to main content

prefer-extracting-function-callbacks

added in: 1.19.0
⚙️
Pro+

Warns when an inline callback is passed as an argument to a function or method invocation.

note

For this rule it's recommended to exclude the test folder.

⚙️ Config

Set allowed-line-count (default is none) to configure the maximum number of lines after which the rule should trigger.

Set ignored-named-arguments (default is empty) to ignore specific named parameters.

Set ignored-invocations (default is empty) to ignore function callbacks in specific invocations.

dart_code_metrics:
...
rules:
...
- prefer-extracting-function-callbacks:
ignored-named-arguments:
- onPressed
allowed-line-count: 3

Example

❌ Bad:

void fn() {
// LINT
listen(onPressed: () {
// Some
// Huge
// Callback
});
}

void listen({
void Function()? onPressed,
}) {}

✅ Good:

void fn() {
listen(onPressed: handlePressed);
}

void handlePressed() {
// Some
// Huge
// Callback
}

void listen({
void Function()? onPressed,
}) {}