Skip to content

Commit 4f03168

Browse files
antfitchparlough
andauthored
Add docs for null-aware element. (#6575)
This add the null-aware element to our [collections documentation](https://dart.dev/language/collections). Notes: This is behind a beta feature flag right now. Preview: https://dart-dev--pr6575-null-aware-element-c-1p89rpex.web.app/language/collections#null-aware-element Fixes [6458](#6458) --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. - [x] This PR doesn't contain automatically generated corrections or text (Grammarly, LLMs, and similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn't use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer. --------- Co-authored-by: Parker Lougheed <parlough@gmail.com>
1 parent 4a8c2cc commit 4f03168

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ignore_for_file: avoid_init_to_null, invalid_null_aware_operator
2+
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
// #docregion code-sample
7+
int? absentValue = null;
8+
int? presentValue = 3;
9+
var items = [
10+
1,
11+
?absentValue,
12+
?presentValue,
13+
absentValue,
14+
5,
15+
]; // [1, 3, null, 5]
16+
// #enddocregion code-sample
17+
18+
print(items);
19+
expect(items, equals([1, 3, null, 5]));
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ignore_for_file: avoid_init_to_null
2+
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
// #docregion code-sample
7+
String? presentKey = 'Apple';
8+
String? absentKey = null;
9+
10+
int? presentValue = 3;
11+
int? absentValue = null;
12+
13+
var itemsA = {presentKey: absentValue}; // {Apple: null}
14+
var itemsB = {presentKey: ?absentValue}; // {}
15+
16+
var itemsC = {absentKey: presentValue}; // {null: 3}
17+
var itemsD = {?absentKey: presentValue}; // {}
18+
19+
var itemsE = {absentKey: absentValue}; // {null: null}
20+
var itemsF = {?absentKey: ?absentValue}; // {}
21+
// #enddocregion code-sample
22+
23+
print(itemsA);
24+
print(itemsB);
25+
print(itemsC);
26+
print(itemsD);
27+
print(itemsE);
28+
print(itemsF);
29+
expect(itemsA, equals({'Apple': null}));
30+
expect(itemsB, equals({}));
31+
expect(itemsC, equals({null: 3}));
32+
expect(itemsD, equals({}));
33+
expect(itemsE, equals({null: null}));
34+
expect(itemsF, equals({}));
35+
}

src/content/language/collections.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ and control flow elements.
277277
* Control flow element: Conditionally or iteratively adds
278278
zero or more values to the surrounding collection.
279279

280+
* Null-aware element: Evaluates an expression and if
281+
the result is not `null`, inserts the value into the
282+
surrounding collection.
283+
280284
* Spread element: Iterates over a given sequence
281285
(collection expression) and inserts all of the
282286
resulting values into the surrounding collection.
@@ -328,6 +332,83 @@ A map entry element has this syntax in a collection:
328332
<key_expression>: <value_expression>
329333
```
330334

335+
### Null-aware elements {: #null-aware-element }
336+
337+
A null-aware element evaluates an expression and if the
338+
result is not `null`, inserts the value into the surrounding
339+
collection.
340+
341+
:::version-note
342+
Null-aware collection elements require
343+
a [language version][] of at least 3.8.
344+
:::
345+
346+
A null-aware element has the following syntax in an
347+
expression element:
348+
349+
```dart
350+
?<expression>
351+
```
352+
353+
A null-aware element has the following syntax in a
354+
map entry element:
355+
356+
```dart
357+
// key is a null-aware element
358+
?<key_expression>: <value_expression>
359+
```
360+
361+
```dart
362+
// value is a null-aware element
363+
<key_expression>: ?<value_expression>
364+
```
365+
366+
```dart
367+
// key and value are null-aware elements
368+
?<key_expression>: ?<value_expression>
369+
```
370+
371+
In the following example, the result for the
372+
null-aware element `?a` is not added to a list called
373+
`items` because `a` is `null`:
374+
375+
<?code-excerpt "misc/test/language_tour/collections/null_aware_element_a.dart (code-sample)"?>
376+
```dart
377+
int? absentValue = null;
378+
int? presentValue = 3;
379+
var items = [
380+
1,
381+
?absentValue,
382+
?presentValue,
383+
absentValue,
384+
5,
385+
]; // [1, 3, null, 5]
386+
```
387+
388+
The following example illustrates various ways that
389+
you can use null-aware elements inside of
390+
map entry elements:
391+
392+
<?code-excerpt "misc/test/language_tour/collections/null_aware_element_b.dart (code-sample)"?>
393+
```dart
394+
String? presentKey = 'Apple';
395+
String? absentKey = null;
396+
397+
int? presentValue = 3;
398+
int? absentValue = null;
399+
400+
var itemsA = {presentKey: absentValue}; // {Apple: null}
401+
var itemsB = {presentKey: ?absentValue}; // {}
402+
403+
var itemsC = {absentKey: presentValue}; // {null: 3}
404+
var itemsD = {?absentKey: presentValue}; // {}
405+
406+
var itemsE = {absentKey: absentValue}; // {null: null}
407+
var itemsF = {?absentKey: ?absentValue}; // {}
408+
```
409+
410+
[language version]: /resources/language/evolution#language-versioning
411+
331412
### Spread elements {: #spread-element }
332413

333414
The spread element iterates over a given sequence and

0 commit comments

Comments
 (0)