Dart Switch Case Statement With Examples

3
20773
dart switch case example
dart switch case statement

Dart switch case statement is a conditional statement like if else ladder statement but it has multiple conditional statements but the only one can be true. Dart switch statement works with various data types. Switch expression can have an integer, enum, Strings or compile-time constant data types.

The syntax of switch statement in Dart looks like this.

  
 
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
   

Dart Switch Case Statement Rules

  • There must be one switch expression but there can be one or N number of cases
  • Every switch case must be unique otherwise it will generate a compile time error.
  • The switch expression and case must have the same data type. The case value should be literal or constant. Variables are not allowed.
  • Each case is a conditional check. If the condition matches the case code block will be executed.
  • In Dart, each case must have ‘break’, ‘continue’, ‘rethrow’, ‘return’ or ‘throw’ as a keyword.
  • The default keyword is also part of the switch statement. If no condition met the default code block will be executed.
  • Boolean and double values cannot be used in switch expression.They are not allowed.

Dart switch case example code

A typical example of switch case statement.

  
 void main() {
  var command = 'WEDNESDAY';
  switch (command) {
    case 'MONDAY':
      print('MONDAY');
      break;
    case 'TUESDAY':
      print('TUESDAY');
      break;
    case 'WEDNESDAY':
      print('WEDNESDAY');
      break;
    case 'THURSDAY':
      print('THURSDAY');
      break;
    case 'FRIDAY':
      print('FRIDAY');
      break;
    default:
      print('It\'s weekend');
  }
}

Out on the console

  
WEDNESDAY

Switch break keyword

The break keyword is not optional in Dart. The break keyword terminates the switch block code. It is necessary every case block must end with ‘break’, ‘continue’, ‘rethrow’, ‘return’ or ‘throw’ as a keyword.

Switch continue keyword

The continue keyword must be followed by a label and the switch code execution will start from that label point.

  
void main() {
  var command = 'TUESDAY';
  switch (command) {
    case 'MONDAY':
      print('MONDAY');
      break;
    case 'TUESDAY':
      print('TUESDAY');
      continue nowClosed;
    nowClosed:
    case 'NOW_CLOSED':
      print('This is now closed');
      break;
    case 'WEDNESDAY':
      print('WEDNESDAY');
      break;
    case 'THURSDAY':
      print('THURSDAY');
      break;
    case 'FRIDAY':
      print('FRIDAY');
      break;
    default:
      print('It\'s weekend');
  }
}

The result of above written code.

  
TUESDAY
This is now closed

The default keyword

When none of the case condition becomes true then default keyword is used. The break keyword is not needed in the default case. This block is used as the last block of the switch statement. Here is the sample

  
void main() {
  var command = 'SATURDAY';
  switch (command) {
    case 'MONDAY':
      print('MONDAY');
      break;
    case 'TUESDAY':
      print('TUESDAY');
      break;
    case 'WEDNESDAY':
      print('WEDNESDAY');
      break;
    case 'THURSDAY':
      print('THURSDAY');
      break;
    case 'FRIDAY':
      print('FRIDAY');
      break;
    default:
      print('It\'s weekend');
  }
}

Output when default block is executed.

  
It's weekend

The switch statement in dart is pretty much simple and easy to understand. I hope I managed to write this tutorial in a very understandable way.

3 COMMENTS

  1. ListView.builder(
    itemBuilder: (BuildContext context, int index) {
    return Container(
    color: (index % 10 == 0) ? Colors.red : Colors.green,
    child: ListTile(
    title: …
    ),
    );
    },
    )

    How can i use switch case to change 10 different colors

    please help…

    • Simply call a function and return a colour value and in the function use switch and define colours. Something like this.

      color:_chooseColor(value),

      _chooseColor(value){

      case 1:
      return Colors.red;
      case 2:
      return Colors.green;

      …..
      }

  2. multiple switch does not create compile time error in dart .try running this
    String days = “sunday”;
    switch(days){
    case “sunday”:print(“Sunday”);
    break;
    case “sunday”:print(“sunday after sunday”);
    break;
    default:print(“default”);
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here