Skip to main content

unnecessary_breaks

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

Unnecessary 'break' statement.

Description

#

The analyzer produces this diagnostic when a break statement appears at the end of a non-empty switch case or default clause. In Dart 3.0 and later, switch cases don't fall through to the next case, so a break at the end of a non-empty case body is redundant.

Example

#

The following code produces this diagnostic because the break is at the end of a non-empty case body:

dart
void f(int x) {
  switch (x) {
    case 1:
      print('one');
      break;
    case 2:
      print('two');
  }
}

Common fixes

#

Remove the break statement:

dart
void f(int x) {
  switch (x) {
    case 1:
      print('one');
    case 2:
      print('two');
  }
}