Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace tip 21 code image
  • Loading branch information
EsteveAguilera committed Nov 10, 2020
commit a0cd66f24b3fe8fb045e37fda92128758a6b1c44
Binary file removed assets/21nullsafecollectioninsert.png
Binary file not shown.
31 changes: 30 additions & 1 deletion page2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,36 @@ We normally use addAll() on collection to add one collection to another.

But From dart 2.3 and above, we can use Spread Operator (`...`) to add collection inside collection.

![nullsfae](assets/21nullsafecollectioninsert.png)
```dart
var numbers = [1, 2, 3];
var names = ["Smith", "Laxman"];
List<int> nullList;
List<int> getLostNumbers() => null;

// This is long way
var list = List();
list.addAll(numbers);
list.addAll(names);

// Hassale to add nullList
list.addAll(nullList ?? []);
list.addAll(getLostNumbers() ?? []);
list.forEach(print);

// This is short way with easy null safe insertion
var list = [...numbers, ...names, ...?nullList, ...?getLostNumbers()];

list.forEach(print);
```
Output:

```
1
2
3
Smith
Laxman
```

[try in dartpad](https://dartpad.dev/98c2ab9d41fb2c20cc67c94956972721)

Expand Down