one_member_abstracts
Avoid defining a one-member abstract class when a simple function will do.
此规则自 Dart 2.0 版本起可用。
详情
#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);
使用方法
#要启用 one_member_abstracts
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 one_member_abstracts
:
linter:
rules:
- one_member_abstracts
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.