You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The null-aware elements language feature enables a simple syntax for
56
-
including an element into a collection only if the element is not
57
-
null. The syntax is available for list elements, set elements, map
58
-
keys, and map values as described in the
59
-
[null-aware elements specification](https://github.com/dart-lang/language/blob/main/accepted/future-releases/0323-null-aware-elements/feature-specification.md).
60
-
61
-
The following is an example of a list literal written in both styles,
62
-
without the null-aware elements language feature and with it:
55
+
The null-aware elements make it easier to omit a value from a collection literal
56
+
if it's `null`. The syntax works in list literals, set literals, and map
57
+
literals. For map literals, both null-aware keys and values are supported. Here
58
+
is an example a list literal written in both styles, without the null-aware
59
+
elements language feature and with it:
63
60
64
61
```dart
65
-
var listWithoutNullAwareElements = [
66
-
if (promotableNullableValue != null) promotableNullableValue,
67
-
if (nullable.value != null) nullable.value!,
68
-
if (nullable.value case var value?) value,
62
+
String? lunch = isTuesday ? 'tacos!' : null;
63
+
64
+
var withoutNullAwareElements = [
65
+
if (lunch != null) lunch,
66
+
if (lunch.length != null) lunch.length!,
67
+
if (lunch.length case var length?) length,
69
68
];
70
69
71
-
var listWithNullAwareElements = [
72
-
?promotableNullableValue,
73
-
?nullable.value,
74
-
?nullable.value,
70
+
var withNullAwareElements = [
71
+
?lunch,
72
+
?lunch.length,
73
+
?lunch.length,
75
74
];
76
75
```
77
76
77
+
Full details are in the [feature specification][null-aware elements].
0 commit comments