Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do use a Switch Case Statement in Dart

I am trying to understand how the switch is working in the dart. I have very simple code:

methodname(num radians) {   switch (radians) {     case 0:       // do something       break;     case PI:       // do something else       break;   } } 

This unfortunately does not work. If left like this the error is: case expressions must have the same type (I think the type is num, but not the editor). If I change 0 to 0.0 it says: The switch type expression double cannot override == operator - I have no idea what this means!

So what is the way to do this switch case? I can turn it onto if/else probably but I wanted to know how to make the switch work and why is it not working in the first place.

I am running the latest stable version of DartEditor.

like image 345
Peter StJ Avatar asked Jun 15 '14 11:06

Peter StJ


People also ask

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.

How do you use a switch-case statement?

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

Can we use switch in case?

You can not do this with switch but there may be a workaround for this. This solution can be extended to be more general. The first element of Tuple can be of type Func<bool> to provide more generic match.


2 Answers

The comparsion of double values using '==' is not very reliable and should be avoided (not only in Dart but in most languages).

You could do something like

methodname(num radians) {   // you can adjust this values according to your accuracy requirements   const myPI = 3142;    int r = (radians * 1000).round();    switch (r) {     case 0:       // do something       break;     case myPI:        // do something else       break;   } } 

This question contains some additional information that might interest you

  • comparing float/double values using == operator
  • How should I do floating point comparison?

some more information:

  • https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.50ae78s6gbw2
  • http://floating-point-gui.de/errors/comparison/
like image 141
Günter Zöchbauer Avatar answered Oct 12 '22 06:10

Günter Zöchbauer


Switch statements in Dart compare integer, string, or compile-time constants using ==. The compared objects must all be instances of the same class (and not of any of its subtypes), and the class must not override ==. Enumerated types work well in switch statements.

Each non-empty case clause ends with a break statement, as a rule. Other valid ways to end a non-empty case clause are a continue, throw, or return statement.

Use a default clause to execute code when no case clause matches:

var command = 'OPEN'; switch (command) {   case 'CLOSED':     executeClosed();     break;   case 'PENDING':     executePending();     break;   case 'APPROVED':     executeApproved();     break;   case 'DENIED':     executeDenied();     break;   case 'OPEN':     executeOpen();     break;   default:     executeUnknown(); } 

The following example omits the break statement in a case clause, thus generating an error:

var command = 'OPEN'; switch (command) {   case 'OPEN':     executeOpen();     // ERROR: Missing break    case 'CLOSED':     executeClosed();     break; } 

However, Dart does support empty case clauses, allowing a form of fall-through:

var command = 'CLOSED'; switch (command) {   case 'CLOSED': // Empty case falls through.   case 'NOW_CLOSED':     // Runs for both CLOSED and NOW_CLOSED.     executeNowClosed();     break; } 

If you really want fall-through, you can use a continue statement and a label:

var command = 'CLOSED'; switch (command) {   case 'CLOSED':     executeClosed();     continue nowClosed;   // Continues executing at the nowClosed label.    nowClosed:   case 'NOW_CLOSED':     // Runs for both CLOSED and NOW_CLOSED.     executeNowClosed();     break; } 

A case clause can have local variables, which are visible only inside the scope of that clause.

like image 45
Paresh Mangukiya Avatar answered Oct 12 '22 05:10

Paresh Mangukiya