Open
Description
This proposal introduces a new syntax feature, the Nullifying Conditional Operator, which can be considered a shorthand for conditional ternary expressions where the else branch is always null
.
Syntax
(please note that the specific syntax of the operator is still being discussed, see the syntax candidates section, ?
will be used as a placeholder in the following examples)
Written in the form a ? b
where:
a
is a Boolean expression.b
is returned ifa
istrue
.- Otherwise, if a is
false
,null
is returned.
Example usage:
final bool hasDrawer = getHasDrawer();
return Scaffold(
drawer: hasDrawer ? drawerWidget,
// equivalent to drawer: hasDrawer ? drawerWidget : null,
...
Where in this example Scaffold would have a non-null drawer
parameter if the boolean flag hasDrawer
evaluates to true
, or null
otherwise.
Syntax candidates
?:
: found too confusing by many?&
?!
?
Use Cases
- Avoids repetitive
null
returns in ternary expressions - Makes conditional expressions more concise
- Keeps code readable without introducing unnecessary verbosity
- Propagating a value based on a Boolean flag
Would love feedback from the community and the Dart team on feasibility and potential implementation!