Skip to content

Instantly share code, notes, and snippets.

@erluxman
Created April 25, 2020 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erluxman/98c2ab9d41fb2c20cc67c94956972721 to your computer and use it in GitHub Desktop.
Save erluxman/98c2ab9d41fb2c20cc67c94956972721 to your computer and use it in GitHub Desktop.
Collection addition with Spread operator
void main() {
var numbers = [1, 2, 3];
var names = ["Smith", "Laxman"];
List<int> nullList;
List<int> getLostNumbers() => null;
//This is long way
print("\n\n\nLong Way");
var list = List();
list.addAll(numbers);
list.addAll(names);
//Hassale to add nullList
list.addAll(nullList??[]);
list.addAll(getLostNumbers()??[]);
list.forEach(print);
print("\n\n\nShort Way");
//This is short way with easy null safe insertion
var list1 = [...numbers, ...names, ...?nullList, ...?getLostNumbers()];
list1.forEach(print);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment