Question

Here's the ugly long code:

var i;
if(true)
  i = 1;
else
  i = 0;

When I try this:

var i = (true ? 0 : 1);

it doesn't work resulting in an error on the following line. I guess I was a bit inattentive reading Dart's syntax specs, so can anybody show me the right way?

Was it helpful?

Solution

This looks perfectly fine from a syntax point of view. You can omit the parentheses.

I get a warning 'Dead code' at '1' with your example because of 'true'.
The Darteditor shows you a hint that you wrote code that may contain a bug because he knows your expression can never evaluate to 1 because of the hardcoded 'true'.

void main(List<String> args) {
    var b = true;
    var i = b ? 0 : 1;
}

doesn't produce a warning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top