Skip to main content

Prefer extracting callbacks

Configurable

Rule id

prefer-extracting-callbacks

Severity

Style

Description

Warns about inline callbacks in a widget tree and suggests to extract them to widget methods in order to make a build method more readable. In addition extracting can help test those methods separately as well.

NOTE the rule will not trigger on:

  • arrow functions like onPressed: () => _handler(...) in order to cover cases when a callback needs a variable from the outside;
  • empty blocks.
  • Flutter specific: arguments with functions returning Widget type (or its subclass) and with first parameter of type BuildContext. "Builder" functions is a common pattern in Flutter, for example, IndexedWidgetBuilder typedef is used in ListView.builder.

Use ignored-named-arguments configuration, if you want to ignore specific named parameters.

Config example

dart_code_metrics:
...
rules:
...
- prefer-extracting-callbacks:
ignored-named-arguments:
- onPressed

Example

Bad:

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextButton(
style: ...,
onPressed: () { // LINT
// Some
// Huge
// Callback
},
child: ...
);
}
}

Good:

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextButton(
style: ...,
onPressed: () => handlePressed(context),
child: ...
);
}

void handlePressed(BuildContext context) {
...
}
}

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextButton(
style: ...,
onPressed: handlePressed,
child: ...
);
}

void handlePressed() {
...
}
}