Ever wondered how to control the flow of your program based on different conditions? In Dart, the switch statement is your friend. In this blog, we will delve into the dart switch statement and how it can be used to control the flow of your program.
A Dart switch statement is a control flow statement that allows the program to evaluate an expression and execute different code blocks based on the value of the expression. The switch statement evaluates an expression against multiple possible values. If the switch expression matches a case value, the corresponding code block is executed.
1switch (expression) { 2 case value1: 3 // Code block 1 4 break; 5 case value2: 6 // Code block 2 7 break; 8 default: 9 // Default code block 10}
The switch statement evaluates an expression and compares it with the values of each case clause. If there is a match, the switch case associated with that value is executed. If no match is found, the default case is executed.
The switch expression is evaluated once. The value of the switch expression is compared with the values of each case. Only constants can be used as a case value in Dart, and each case must be unique.
1var fruit = 'Apple'; 2 3switch (fruit) { 4 case 'Apple': 5 print('Apple is good for health'); 6 break; 7 case 'Banana': 8 print('Banana is rich in potassium'); 9 break; 10 default: 11 print('All fruits are good for health'); 12}
In this example, the switch expression is fruit, which is a variable with the value 'Apple'. The switch statement compares fruit with the case values 'Apple' and 'Banana'. Since fruit matches the case value 'Apple', it prints 'Apple is good for health'.
The case clauses in a switch statement specify the code block to be executed when the switch expression matches the case value. Each case clause ends with a break statement to prevent fallthrough to the next case.
The default case in a switch statement specifies the code block to be executed when the switch expression does not match any case value. It's like a guard clause for the switch statement.
1var color = 'Yellow'; 2 3switch (color) { 4 case 'Red': 5 print('Red signifies danger'); 6 break; 7 case 'Green': 8 print('Green signifies go'); 9 break; 10 default: 11 print('Color not recognized'); 12}
In this example, the switch expression is color, which is a variable with the value 'Yellow'. The switch statement compares color with the case values 'Red' and 'Green'. Since the color doesn't match any of the case values, it executes the default case and prints 'Color not recognized'.
In Dart, switch statements can be used with Enum. Enum in Dart is a special kind of class used to represent a fixed number of constant values.
1enum Seasons { Spring, Summer, Autumn, Winter } 2 3void main() { 4 var season = Seasons.Summer; 5 6 switch (season) { 7 case Seasons.Spring: 8 print('Spring is here'); 9 break; 10 case Seasons.Summer: 11 print('Summer is hot'); 12 break; 13 default: 14 print('It\'s either Autumn or Winter'); 15 } 16}
In this example, we have an Enum Seasons with four constant values: Spring, Summer, Autumn, and Winter. We then create a variable season with the value Seasons.Summer. The switch statement compares season with the case values Seasons.Spring and Seasons.Summer. Since season matches the case value Seasons.Summer, it prints 'Summer is hot'.
In Dart, if a switch statement doesn't cover all possible values and doesn't include a default case, it results in a compile-time error. To handle this, we can use a default case to catch any invalid choice.
1var animal = 'Lion'; 2 3switch (animal) { 4 case 'Dog': 5 print('Dog is a pet'); 6 break; 7 case 'Cat': 8 print('Cat is a pet'); 9 break; 10 default: 11 print('Invalid choice'); 12}
In this example, the switch expression is animal, which is a variable with the value 'Lion'. The switch statement compares animal with the case values 'Dog' and 'Cat'. Since animal doesn't match any of the case values, it executes the default case and prints 'Invalid choice'.
In Dart, switch statements can also be used with classes. However, the switch expression must return a constant value at compile time.
1class Vehicle { 2 final String type; 3 4 const Vehicle(this.type); 5} 6 7void main() { 8 var vehicle = const Vehicle('Car'); 9 10 switch (vehicle.type) { 11 case 'Car': 12 print('Car is a four wheeler'); 13 break; 14 case 'Bike': 15 print('Bike is a two wheeler'); 16 break; 17 default: 18 print('Vehicle type not recognized'); 19 } 20}
In this example, we have a class Vehicle with a final string field type. We then create a constant instance of Vehicle with the type 'Car'. The switch statement compares vehicle.type with the case values 'Car' and 'Bike'. Since vehicle.type matches the case value 'Car', it prints 'Car is a four wheeler'.
The dart switch statement is a powerful tool for controlling the flow of your program. It allows you to evaluate an expression and execute different code blocks based on the value of the expression. Remember to always include a default case in your switch statements to handle any invalid choice and prevent compile-time errors.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.