Ternary Operator in Dart is an alternative way to control the logic flow like IF, else statement.
Ternary Conditional Operators are a pretty useful thing in Dart. They can be used to conditionally show a Flutter widget. They accept 2 operands.
The ternary operator takes three arguments, and it’s usually used when only two actions are needed. The ternary operator checks the first argument for comparison, the second is the action if the argument is true, and the third is the action if the argument is false.
This will look familiar to you from the “Operators” section’s conditional expressions because it’s used often to make code flow decisions.
// Shorter way of if and else statement isClosed ? askToOpen() : askToClose();
The ternary operator is right-associative, which means it can be “chained” to check multiple conditions in turn.
condition1 ? value1 : condition2 ? value2 : condition3 ? value3 : condition4 ? value4 : condition5 ? value5 // and so on : valueN;
int mark = 70; String result = mark > 80 ? 'A+' : mark >70 ? 'A' : mark > 60 ? 'A-' : mark >50 ? 'B' : 'F'; print(result);