Cequiel Cequiel - 3 months ago 10
Dart Question

Use the => operator in boolean expressions

How could I use the "=>" operator in boolean expressions? For example, the following declaration is wrong, since "isOpen = !isOpen" returns a boolean value:

// not a good function declaration
void toggle => isOpen = !isOpen;


I was thinking in something similar to this:

void toggle => shutUp(isOpen = !isOpen);


Where
shutUp()
is a function that returns a
void
value and accepts any boolean parameter.

Answer

Like this:

var isOpen = false;
var toggle = () => isOpen = !isOpen;
toggle();
print(isOpen);
toggle();
print(isOpen);