Skip to main content

exhaustive_cases

Details about the 'exhaustive_cases' diagnostic produced by the Dart analyzer.

Missing case clauses for some constants in '{0}'.

Description

#

The analyzer produces this diagnostic when a switch statement over an instance of an enum-like class is missing a case clause for one or more of the class's constants and doesn't have a default clause.

Enum-like classes are non-abstract classes that have:

  • Only private, non-factory constructors.
  • Two or more static const fields whose type is the enclosing class.
  • No subclasses in the defining library.

Example

#

The following code produces this diagnostic because the switch statement is missing a case clause for the constant C.c:

dart
class const C._(final int i) {
  static const C a = ._(1);
  static const C b = ._(2);
  static const C c = ._(3);
}

void f(C c) {
  switch (c) {
    case .a:
      print('a');
    case .b:
      print('b');
  }
}

Common fixes

#

If the missing constant needs to be handled separately, then add a case clause for it:

dart
class const C._(final int i) {
  static const C a = ._(1);
  static const C b = ._(2);
  static const C c = ._(3);
}

void f(C c) {
  switch (c) {
    case .a:
      print('a');
    case .b:
      print('b');
    case .c:
      print('c');
  }
}

If the missing constant doesn't need to be handled specially, then add a default clause to cover it:

dart
class const C._(final int i) {
  static const C a = ._(1);
  static const C b = ._(2);
  static const C c = ._(3);
}

void f(C c) {
  switch (c) {
    case .a:
      print('a');
    case .b:
      print('b');
    default:
      print('other');
  }
}