? :), commonly known as the ternary operator, is an expression that evaluates a boolean condition and returns the result of one of two expressions depending on whether the condition is true or false. It serves as a concise, expression-level alternative to the if-else statement.
Syntax
Operands
condition: An expression that must evaluate to a type ofbool.expression1: The expression evaluated and returned ifconditionistrue.expression2: The expression evaluated and returned ifconditionisfalse.
Evaluation Logic
The operator utilizes short-circuit evaluation. The runtime process occurs in the following order:- The
conditionis evaluated. - If the
conditionistrue,expression1is evaluated and its value becomes the result of the operation.expression2is not evaluated. - If the
conditionisfalse,expression2is evaluated and its value becomes the result of the operation.expression1is not evaluated.
Type Inference
The static type of the entire conditional expression is determined by the least upper bound (LUB) of the types ofexpression1 and expression2.
- If both expressions are of type
int, the result isint. - If one expression is
intand the other isdouble, the result isnum. - If the types share no common hierarchy other than
Object?, the result isObject?(ordynamicdepending on context).
Example
Master Dart with Deep Grasping Methodology!Learn More





