Skip to content

Null-aware elements #323

Closed
Closed
@alorenzen

Description

@alorenzen

Admin comment:

This is being implementated: feature specification, implementation issue, experiment flag null-aware-elements.

Null-aware elements offer a shorthand for adding an element to a list, if the element is non-null. Consider this code before the feature:

Stack(
  fit: StackFit.expand,
  children: [
    const AbsorbPointer(),
    if (widget.child != null) widget.child!,
  ],
)

The causes repetition of widget.child, and is verbose. With null-aware elements this becomes:

Stack(
  fit: StackFit.expand,
  children: [
    const AbsorbPointer(),
    ?widget.child,
  ],
)

Original issue contents:

I would love to have a "null-aware" operator for adding an element to a list if it is non-null.

[
  foo,
  ?bar,
]

This would be equivalent to:

[
  foo,
  if (bar != null) bar,
]

I've been trying out the new "UI-as-code" features in the angular codebase, and find that I can't convert a common pattern to use the new element syntax.

We often iterate over a list, adding elements to a new list when the value is non-null.

var result = [];
for (var node in nodes) {
  var value = convert(node);
  if (value != null) result.add(value);
}

It would be pretty nice if I could instead use the new syntax:

var result = [
  for (var node in nodes) ?convert(node);
]

Metadata

Metadata

Assignees

Labels

featureProposed language feature that solves one or more problemssmall-featureA small feature which is relatively cheap to implement.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions