Dart switch statement - Case expressions must be constant

11,875

Solution 1

The switch case expressions must be constants for sure.

You have to use if/then chains to do multiple tests based on non-constant values.

You cannot use arguments from the surrounding function in a switch case. What you are trying to do here is not supported by the Dart switch statement.

The Dart switch statement is deliberately kept very simple, so that a compiler can know all the possible cases at compile-time. That's why they must be compile-time constants.

Switch statements are still useful for some kinds of switching, like on enums:

enum Nonse { foo, bar, baz; }

String fooText(Nonse non) {
  switch (non) {
    case Nonse.foo: return "foo";
    case Nonse.bar: return "bar";
    case Nonse.baz: return "baz";
  }
  throw ArgumentError.notNull("non");
}

You can also switch over constant string values or integer values.

Solution 2

There are some rules for Switch Case

The default case is optional.

All case expression must be unique.

The case statements can include only constants. It cannot be a variable or an expression.

The data type of the variable and the case expression must match.

There can be any number of case statements within a switch.

you should use 'If Else' statement

Solution 3

Switch statement requires a constant -> it means already initialized variable with final value

switch (expression) {
  case ONE : {
      statement(s);
  }
  break;
  case TWO: {
      statement(s);
  }
  break;
  default : {
      statement(s);
  }
}
Share:
11,875
Hasen
Author by

Hasen

Updated on July 25, 2022

Comments

  • Hasen
    Hasen almost 2 years

    I looked at a few questions regarding this for other languages and some suggest using final but that doesn't seem to work with Dart.

    I'm passing in arguments so surely the switch statement cannot contain constants only? A switch statement, much like an if statement is asking if it is or not..ie it's unknown so I don't see how a switch statement can be useful if they have to be constants...?

      setCategory(arga, argb) {
        int result;
        switch (true) {
          case (arga >= 0 && arga < 3 && argb < 35):
            result = 16;
            break;
          case (arga >= 0 && arga < 3 && argb >= 35):
            result = 15;
            break;
            etc
    

    It's returning the error Case expressions must be constant regarding the values arga and argb in the case expressions. What's the best way to remedy this or do I have to use an if statement?

  • Harsh Patel
    Harsh Patel almost 5 years
    Actually its not useless but you are using it wrongly.
  • Hasen
    Hasen almost 5 years
    Constant values never change...I don't see the point in asking about them in a switch statement.
  • Ryde
    Ryde over 3 years
    Do you know how to work with enum extension too?
  • lrn
    lrn over 3 years
    Extension methods on an enum type works just like extension methods on any other type. You can't make extensions on individual values, so you'll still have to switch on the this value. So, nothing different about that for this question.