目次

avoid_classes_with_only_static_members

静的メンバーのみを含むクラスの定義を避けてください。

このルールはDart 2.0から利用可能です。

詳細

#

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