prefer_switch_case

Group: style

Maturity: stable

Code Sharp v0.0.1

Since info is static, may be stale
recommendedflutter

View all Lint Rules

Using the Linter

Prefer switch case instead of multiple if else processes.

BAD:


void action(int code) {
  if (code == 0) {
    // doSomething();
  } else if (code == 1) {
    // doSomething()
  } else if (code == 2) {
    // doSomething()
  } else if (code == 3) {
    // doSomething()
  } else {
    // doSomething()
  }
}

GOOD:

void action(int code) {
  switch (code) {
    case 0:
      // doSomething();
      break;
    case 1:
      // doSomething();
      break;
    case 2:
      // doSomething();
      break;
    case 3:
      // doSomething();
      break;
    default:
    // doSomething();
  }
}