跳到主要内容

avoid_classes_with_only_static_members

稳定

避免定义仅包含静态成员的类。

详情

#

来自 Effective 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