?: operator, formally known as the conditional operator or ternary operator, is an expression-based control flow construct in Dart. It evaluates a boolean condition and returns the result of one of two subsequent expressions based on that boolean value, effectively acting as a single-line, expression-level if-else statement.
- The
conditionis evaluated first. In Dart’s sound null safety system, this expression must strictly evaluate to a type ofbool. - If the
conditionevaluates totrue, theconsequentExpressionis evaluated, and its result becomes the value of the entire ternary operation. - If the
conditionevaluates tofalse, thealternativeExpressionis evaluated, and its result becomes the value of the entire ternary operation.
?: operation is determined by the least upper bound (LUB) of the static types of the consequentExpression and the alternativeExpression.
?: operators are chained without explicit parentheses, the syntax tree groups them from right to left. However, runtime evaluation strictly occurs left-to-right. The leftmost condition is evaluated first, and short-circuiting dictates whether the nested rightward expressions are evaluated at all.
expressionWithoutCascade. Consequently, using an unparenthesized cascade operator (.. or ?...) inside a ternary operation results in a syntax error. To use a cascade within the consequentExpression or alternativeExpression, the cascade operation must be explicitly wrapped in parentheses.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





