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.
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.
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.
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.
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
some more information:
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.
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