one_member_abstracts
Avoid defining a one-member abstract class when a simple function will do.
This rule is available as of Dart 2.0.
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);
Usage
#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
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2024-07-03. View source or report an issue.