Switch case with multiple values for the same case
Asked Answered
S

5

55

I would like to know the syntax to set a multiple case statement in a switch / case.
For example :

String commentMark(int mark) {
    switch (mark) {
        case 0 : // Enter this block if mark == 0
            return "Well that's bad" ;
        case 1, 2, 3 : // Enter this block if mark == 1 or mark == 2 or mark == 3
            return "Gods what happend" ;
        // etc.
        default :
            return "At least you tried" ;
    }
}

I cannot find the right syntax to set multiple case (the line case 1, 2, 3 :), is it even possible in Dart ?

I did not found any informations on pub.dev documentation, neither on dart.dev.

I tried :
case 1, 2, 3
case (1, 2, 3)
case (1 ; 2 ; 3)
case (1 : 2 : 3)
case 1 : 3
and more !

Sonjasonnet answered 12/11, 2019 at 11:37 Comment(1)
try using 1...3Preshrunk
U
122

As of Dart 3, break; is not necessary for non-empty case blocks. For empty blocks, you can list cases one after the other to get the following code execute on either one of those cases.

String commentMark(int mark) {
    switch (mark) {
        case 0 : // Enter this block if mark == 0
            return "mark is 0" ;
        case 1:
        case 2:
        case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
            return "mark is either 1, 2 or 3" ;
        // etc.
        default :
            return "mark is not 0, 1, 2 or 3" ;
    }
}

Before Dart 3, if you did not want to return, you had to use break; after each block. But you don't have to do that anymore. Therefore this code below is equivalent to the one above.

String commentMark(int mark) {
    String msg;
    switch (mark) {
        case 0 : // Enter this block if mark == 0
            msg = "mark is 0" ;
        case 1:
        case 2:
        case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
            msg = "mark is either 1, 2 or 3" ;
        // etc.
        default:
            msg = "mark is not 0, 1, 2 or 3" ;
    }
    return msg;
}

Also with Dart 3 came the || syntax, so this is also equivalent.

String commentMark(int mark) {
    String msg;
    switch (mark) {
        case 0 : // Enter this block if mark == 0
            msg = "mark is 0" ;
        case 1 || 2 || 3:
            msg = "mark is either 1, 2 or 3" ;
        // etc.
        default:
            msg = "mark is not 0, 1, 2 or 3" ;
    }
    return msg;
}

Furthermore, Dart 3 introduces the switch expression, so the whole thing can also become this:

String commentMark(int mark) {
    return switch (mark) {
      0 => "mark is 0",
      1 || 2 || 3 => "mark is either 1, 2 or 3",
      _ => "mark is not 0, 1, 2 or 3",
    };
}
Unitary answered 12/11, 2019 at 11:38 Comment(1)
Weird that today I get an error without "break;" => The 'case' shouldn't complete normally. (Documentation) Try adding 'break', 'return', or 'throw'.Duodenitis
S
10

Dart 3.0 have Pattern feature which you can use to make it simple.

For details see the documentation Link https://dart.dev/language/patterns

Solution 1:

String commentMark(int mark) {
    return switch (mark) {
        0 => "mark is 0",
        1 || 2 || 3 => "mark is either 1, 2 or 3",
        _ => "mark is not 0, 1, 2 or 3"
   };
}

print(commentMark(2)) ;

Solution 2:

  String commentMark(int mark) {
      switch (mark) {
        case 0:
          return "mark is 0";
        case 1 || 2 || 3:
          return "mark is either 1, 2 or 3";
        default:
         return "mark is not 0, 1, 2 or 3";
      }
  }

print(commentMark(3));

Splitting answered 15/9, 2023 at 12:24 Comment(0)
A
0

Instead of multiple case we can use or operator in single switch case it self.

switch (date) {
        case 1 | 21 | 31:
            return "st";
        case 2 | 22:
            return "nd";
        case 3 | 23:
            return "rd";
        default:
            return "th";
        }
Atalie answered 26/4, 2022 at 13:32 Comment(2)
In Dart, case 1 | 21 | 31: is equivalent to case 31: because 1 | 21 | 31 is evaluated as 31. So the case statement doesn't match the numbers 1 and 21.Jaela
This case will not work. In this, all cases are considered as the last value. So Instead of this, you should write all cases separately. And use break; after the all cases.Affined
A
0

If you want to use multiple cases you should use below syntax:

     switch (yourCase) {    
            case 1:
            case 2:
            case 3: 
                  // Enter this block if yourCase == 1 or yourCase == 2 or yourCase == 3
                var msg = "your code to for cases either 1, 2 or 3" ;
                break;
            case 4: 
                  // Enter this block if yourCase == 4
                break;
                  }
Affined answered 18/4, 2023 at 12:8 Comment(0)
R
0

Its simple, you have to provide all required enums together as shown below

switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return androidHandler;
      case TargetPlatform.iOS:
        return iosHandler;
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
      case TargetPlatform.linux:
      case TargetPlatform.fuchsia:
        throw UnsupportedError(
          'App  not supported for ${defaultTargetPlatform.name}',
        );
    }

Dart doesn't need explicit break So above code will throw UnsupportedError for macOS, windows,linux and fuchsia.

Raimundo answered 27/6 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.