one_ member_ abstracts
Learn about the one_member_abstracts linter rule.
Avoid defining a one-member abstract class when a simple function will do.
Details
#NOTE: As one member abstracts are often a stepping stone, this lint rule has been deprecated as of Dart 3.13 and is set to be removed in a future release of the Dart SDK. Remove all inclusions of this lint from your analysis options. If you wish to keep enforcing it, consider implementing the rule with an analyzer plugin.
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.12.2. Report an issue.