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
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。