Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Switch with range

Tags:

dart

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;
}
like image 936
Eike Avatar asked Mar 23 '18 14:03

Eike


People also ask

Can you do ranges in switch statements?

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.

Does Dart have switch case?

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.

What is switch case in Dart?

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.


1 Answers

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";
}
like image 194
Rémi Rousselet Avatar answered Oct 11 '22 13:10

Rémi Rousselet