目录

avoid_classes_with_only_static_members

Avoid defining a class that contains only static members.

此规则自 Dart 2.0 版本起可用。

详情

#

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:

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';
}

GOOD:

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