Switch case with multiple values for the same case

Ely*_*nad 2 switch-statement dart flutter

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" ;
    }
}
Run Code Online (Sandbox Code Playgroud)

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.

我尝试过:
case 1, 2, 3
case (1, 2, 3)
case (1 ; 2 ; 3)
case (1 : 2 : 3)
case 1 : 3
还有更多!

Tar*_*ali 8

Dart 3.0 具有模式功能,您可以使用它来使其变得简单。

有关详细信息,请参阅文档链接https://dart.dev/language/patterns

解决方案一:

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"
   };
}
Run Code Online (Sandbox Code Playgroud)

打印(评论标记(2));

解决方案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";
      }
  }
Run Code Online (Sandbox Code Playgroud)

打印(评论标记(3));


Gaz*_*kus 5

这应该做

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

Run Code Online (Sandbox Code Playgroud)