I have a lot of ternary operators in my code, for instance:
height: object.isSmall ? 30.0 : 40.0,
Sometimes even with two ternarys in one:
color: object.small ? Colors.blue : object.done ? Colors.red : Colors.green
Since these code snippets are within my 'build Function', it is very hard to read and just does not look right/good.
Is there an elegant way to solve this problem? I thought of 'Maps' but I really do not have a clue to approach this.
Maybe you have an idea. Thank you!
CodePudding user response:
You could extract your logic into functions and use proper if-else statements:
color: _buildColor(),
...
Color _buildColor() {
if (object.small) {
return Colors.blue;
} else if (object.done) {
return Colors.red;
} else {
return Colors.green;
}
CodePudding user response:
if it is more convenient for you, try the switch
switch (object.size) {
case small:
return Colors.blue;
break
case done:
return Colors.red;
default:
return Colors.green;
}