Introduction
In this blog, we will look into the Dart Fallthrough. Dart Fallthrough is an error that is generated in switch-case statements. We will discuss how and when this error is generated.
If you are new to Dart and are yet to set up your environment for the language, you can check out this article.
Dart Fallthrough
We get a Fallthrough error when we forget to include a break statement in our switch-case statements. The control flow shifts to the following line in that instance. If no break appears, control will flow through all cases that follow the true case until the break or the switch's end is reached.
Skipping Break Statement
The most obvious way of creating a Fallthrough error is by skipping the break statements. This method works in many languages except Dart. Dart will throw a compilation error in such a case. For example,
// Main function to run the program
void main() {
String operator = "+";
// switch-case statements
switch (operator) {
case "+": // Addition Operator
{
print("This is the Addition operator.");
}
case "-": // Subtraction Operator
{
print("This is the Subtraction operator.");
}
case "*": // Multiplication Operator
{
print("This is the Multiplication operator.");
}
case "/": // Division Operator
{
print("This is the Division operator.");
}
default: // Default case
{
print("This is the default case.");
}
}
}
Output:
main.dart:7:5: Error: Switch case may fall through to the next case.
case "+": // Addition Operator
^
main.dart:11:5: Error: Switch case may fall through to the next case.
case "-": // Subtraction Operator
^
main.dart:15:5: Error: Switch case may fall through to the next case.
case "*": // Multiplication Operator
^
main.dart:19:5: Error: Switch case may fall through to the next case.
case "/": // Division Operator
Empty Case statement
Dart supports empty case statement. Empty cases will fall through to the next statement. For example,
// Main function to run the program
void main() {
String operator = "*";
// switch-case statements
switch (operator) {
case "+": // Addition Operator
{
print("This is the Addition operator.");
break;
}
case "-": // Subtraction Operator
{
print("This is the Subtraction operator.");
break;
}
case "*": // Multiplication Operator // Empty case
case "/": // Division Operator
{
print("This is the Division operator.");
break;
}
default: // Default case
{
print("This is the default case.");
}
}
}
Output:
This is the Division operator.
As you can see in the above example, when the condition reaches the empty case statement, it will Fallthrough and execute the following case statement. In this case, as the case statement for "*" was empty, it executed the print statement for the next case(i.e., "/").
Replace break with a continue statement
One other way of achieving Fallthrough in Dart is to use a continue instead of a break statement. For example,
// Main function to run the program
void main() {
String operator = "-";
// switch-case statements
switch (operator) {
case "+": // Addition Operator
{
print("This is the Addition operator.");
break;
}
case "-": // Subtraction Operator
{
print("This is the Subtraction operator.");
continue nextStatement; // continue to the next statement
}
nextStatement: case "*": // Multiplication Operator
{
print("This is the Multiplication operator.");
break;
}
case "/": // Division Operator
{
print("This is the Division operator.");
break;
}
default: // Default case
{
print("This is the default case.");
break;
}
}
}
Output:
This is the Subtraction operator.
This is the Multiplication operator.




