Is there a way in dart to switch between ranges. I couldn't find anything except clamp
For example:
switch(response.statusCode) {
case 200..300: return "OK";
case 400..500: return "Error";
default: break;
}
Using range in switch case in C/C++ In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement. After writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.
In Dart, switch-case statements are a simplified version of the nested if-else statements. Its approach is the same as that in Java. The default case is the case whose body is executed if none of the above cases matches the condition.
Dart Switch case statement is used to avoid the long chain of the if-else statement. It is the simplified form of nested if-else statement. The value of the variable compares with the multiple cases, and if a match is found, then it executes a block of statement associated with that particular case.
Not possible with switch. Use if/else if
with >=
and <=
to achieve the same result.
if (response.statusCode >= 200 && response.statusCode <= 300) {
return "OK";
}
else if (response.statusCode >= 400 && response.statusCode <= 500) {
return "Error";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With