use_enums

Group: style

Maturity: stable

Dart SDK: >= 2.17.0 • (Linter v1.19.0)

Since info is static, may be stale
has-fix

View all Lint Rules

Using the Linter

Classes that look like enumerations should be declared as enums.

DO use enums where appropriate.

Candidates for enums are classes that:

BAD:

class LogPriority {
  static const error = LogPriority._(1, 'Error');
  static const warning = LogPriority._(2, 'Warning');
  static const log = LogPriority._unknown('Log');

  final String prefix;
  final int priority;
  const LogPriority._(this.priority, this.prefix);
  const LogPriority._unknown(String prefix) : this._(-1, prefix);
}

GOOD:

enum LogPriority {
  error(1, 'Error'),
  warning(2, 'Warning'),
  log.unknown('Log');

  final String prefix;
  final int priority;
  const LogPriority(this.priority, this.prefix);
  const LogPriority.unknown(String prefix) : this(-1, prefix);
}

Usage

To enable the use_enums lint, add use_enums under linter > rules in your analysis_options.yaml file:

linter:
  rules:
    - use_enums