one_member_abstracts
Avoid defining a one-member abstract class when a simple function will do.
Details
#From Effective Dart:
AVOID defining a one-member abstract class when a simple function will do.
Unlike Java, Dart has first-class functions, closures, and a nice light syntax for using them. If all you need is something like a callback, just use a function. If you're defining a class and it only has a single abstract member with a meaningless name like call
or invoke
, there is a good chance you just want a function.
BAD:
abstract class Predicate {
bool test(item);
}
GOOD:
typedef Predicate = bool Function(item);
Enable
#To enable the one_member_abstracts
rule, add one_member_abstracts
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- one_member_abstracts
If you're instead using the YAML map syntax to configure linter rules, add one_member_abstracts: true
under linter > rules:
linter:
rules:
one_member_abstracts: true
Unless stated otherwise, the documentation on this site reflects Dart 3.6.2. Page last updated on 2025-01-27. View source or report an issue.