Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Switch


Share

Dart Switch

Instead of writing many if...else statements, you can use the switch statement.

The switch statement picks one of many code blocks to run:

switch (value) {
  case a:
    // code block
  case b:
    // code block
  default:
    // code block
}

This is how it works:

  • Dart reads the switch value once
  • It compares the value with each case, from top to bottom
  • If there is a match, Dart runs the code of that case, and then leaves the switch
  • If no case matches, Dart runs the default code, if there is one

A Switch Example

This example uses the weekday number to print the weekday name:

Example

void main() {
  final day = 4;
  switch (day) {
    case 1:
      print('Monday');
    case 2:
      print('Tuesday');
    case 3:
      print('Wednesday');
    case 4:
      print('Thursday');
    case 5:
      print('Friday');
    case 6:
      print('Saturday');
    case 7:
      print('Sunday');
  }
}
Try it Yourself »

Result:

Thursday

Only "Thursday" is printed.

When a case has code, Dart stops after that code and does not run the next case.

So in Dart, you do not need a break at the end of each case.

Note: In languages like Java and C, a case without break "falls through" and runs the next case too.

Dart does not do this.

You may still write break, but it is not needed.



The default Keyword

The default keyword gives the code to run when no case matches:

Example

void main() {
  final day = 9;
  switch (day) {
    case 6:
      print('Today is Saturday');
    case 7:
      print('Today is Sunday');
    default:
      print('Looking forward to the weekend');
  }
}
Try it Yourself »

Result:

Looking forward to the weekend

The default case must be the last case in the switch.


Cases That Share Code

An empty case, a case with no code, uses the code of the next case.

This lets several values share the same code:

Example

void main() {
  final day = 'Sunday';
  switch (day) {
    case 'Saturday':
    case 'Sunday':
      print('Weekend');
    default:
      print('Weekday');
  }
}
Try it Yourself »

Result:

Weekend

You can also put several values in one case with ||, which means "or":

Example

void main() {
  final fruit = 'lemon';
  switch (fruit) {
    case 'apple' || 'cherry' || 'strawberry':
      print('$fruit is red');
    case 'banana' || 'lemon':
      print('$fruit is yellow');
    default:
      print('I do not know the color of $fruit');
  }
}
Try it Yourself »

Result:

lemon is yellow

Switch With Strings

The switch value can be a number, a string, or many other types.

Each case runs as many statements as you need:

Example

void main() {
  final command = 'start';
  switch (command) {
    case 'start':
      print('Starting the engine...');
      print('The car is running.');
    case 'stop':
      print('Stopping the engine...');
      print('The car is parked.');
    default:
      print('Unknown command: $command');
  }
}
Try it Yourself »

Result:

Starting the engine...
The car is running.

Switch Expressions

Dart also has a switch expression, a shorter form of switch that gives back a value.

You will learn about it in the Dart Switch Expressions chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.