avoid_classes_with_only_static_members
避免定义仅包含静态成员的类。
详情
#避免 定义仅包含静态成员的类。
不鼓励创建仅为了提供实用程序或静态方法的类。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
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。