Like any language, Dart also supports Switch case statements.

Switch executes expression and matches result with case constants and executes case block statements.

Here is the syntax of a simple if statement.

switch(conditional_expression) {
   case constant1: {
      // statements;
   }
   break;

   case constant2: {
      //statements;
   }
   break;

   default: {
      //default case statements;
   }
   break;
}

conditional_expression is evaluated to value, value is compared with case constant, and value and matched case statements are executed.

conditional_expression can be a variable dart expression or enum value.

if no matching case is found, the default case is executed.

Some important points

  • case constant must be a compile-time constant, It can not be variable, and the expression

In this below example, the case variable contains the variable instead of the constant value. It throws Error: Not a constant expression in switch case.

void main() {
  int number = 50;
  int variable = 45;

  switch (number) {
    case variable:
      {
        print("50");
      }
      break;

    default:
      {
        print("default");
      }
      break;
  }
}
  • switch expression result type and case constant type must match or subtype of switch expression type.

In the below example,

The switch expression type is int and the first case constant type is a string. It throws Error: Type ‘String’ of the case expression is not a subtype of type ‘int’ of this switch expression.

void main() {
  int number = 50;

  switch (number) {
    case "string":
      {
        print("50");
      }
      break;
    default:
      {
        print("default");
      }
      break;
  }
}
  • if there is no break in the case block, It throws Error: Switch case may fall through to the next case.

  • There is no limit on the number of case blocks

  • The default case is optional.

  • Also, if case constants contain duplicates, It gives a warning Do not use more than one case with the same value

Dart Switch case examples

In this example, the switch expression value is 50 and the matching case is executed. case 50 is selected and print 50, break causes to exit from switch case flow.

Here are Switch case examples

void main() {
  int number = 50;

  switch (number) {
    case 50:
      {
        print("50");
      }
      break;
    case 60:
      {
        print("60");
      }
      break;

    default:
      {
        print("default");
      }
      break;
  }
}

Output:

50

Switch Enum case example

This example uses the Enum variable in switch expression and Enum value in case statements.

Enum is a custom type defined in the dart, used to define fixed constants. In the below program,

  • Create an Enum constant with three values.
  • Declare a variable of Enum.
  • used variable in the switch expression
  • Enum value is being used in a case statement.
enum NUMBER { ONE, TWO, THREE }

void main() {
  var number = NUMBER.ONE;
  switch (number) {
    case NUMBER.ONE:
      print("one");
      break;
    case NUMBER.TWO:
      print("two");
      break;
    case NUMBER.THREE:
      print("three");
      break;
  }
}

Output:

one