avoid_classes_with_only_static_members
Avoid defining a class that contains only static members.
Details
#From Effective Dart:
AVOID defining a class that contains only static members.
Creating classes with the sole purpose of providing utility or otherwise static methods is discouraged. Dart allows functions to exist outside of classes for this very reason.
BAD:
class DateUtils {
static DateTime mostRecent(List<DateTime> dates) {
return dates.reduce((a, b) => a.isAfter(b) ? a : b);
}
}
class _Favorites {
static const mammal = 'weasel';
}
GOOD:
DateTime mostRecent(List<DateTime> dates) {
return dates.reduce((a, b) => a.isAfter(b) ? a : b);
}
const _favoriteMammal = 'weasel';
Enable
#To enable the avoid_classes_with_only_static_members
rule,
add avoid_classes_with_only_static_members
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- avoid_classes_with_only_static_members
If you're instead using the YAML map syntax to configure linter rules,
add avoid_classes_with_only_static_members: true
under linter > rules:
linter:
rules:
avoid_classes_with_only_static_members: true
除非另有说明,文档之所提及适用于 Dart 3.7.0 版本,本页面最后更新时间: 2025-01-27。 查看文档源码 或者 报告页面问题。