避免只包含静态成员的类
避免定义只包含静态成员的类。
详情
#摘自 《高效 Dart》
避免定义只包含静态成员的类。
不鼓励创建仅用于提供工具方法或其他静态方法的类。Dart 允许函数存在于类之外,正是为此目的。
不好
dart
class DateUtils {
static DateTime mostRecent(List<DateTime> dates) {
return dates.reduce((a, b) => a.isAfter(b) ? a : b);
}
}
class _Favorites {
static const mammal = 'weasel';
}
好
dart
DateTime mostRecent(List<DateTime> dates) {
return dates.reduce((a, b) => a.isAfter(b) ? a : b);
}
const _favoriteMammal = 'weasel';
启用
#要启用 avoid_classes_with_only_static_members
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 avoid_classes_with_only_static_members
analysis_options.yaml
yaml
linter:
rules:
- avoid_classes_with_only_static_members
如果你使用的是 YAML 映射语法来配置 Linter 规则,请在 linter > rules 下添加 avoid_classes_with_only_static_members: true
analysis_options.yaml
yaml
linter:
rules:
avoid_classes_with_only_static_members: true