How do I combine two lists in Dart?
You can use:
var newList = new List.from(list1)..addAll(list2);
If you have several lists you can use:
var newList = [list1, list2, list3].expand((x) => x).toList()
As of Dart 2 you can now use +
:
var newList = list1 + list2 + list3;
As of Dart 2.3 you can use the spread operator:
var newList = [...list1, ...list2, ...list3];
maybe more consistent~
var list = []..addAll(list1)..addAll(list2);
If you want to merge two lists and remove duplicates could do:
var newList = [...list1, ...list2].toSet().toList();
Dart now supports concatenation of lists using the +
operator.
Example:
List<int> result = [0, 1, 2] + [3, 4, 5];