I was wondering if there was an easy way to concatenate two lists in dart to create a brand new list object. I couldn't find anything and something like this:
My list:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
I tried:
var newList = list1 + list2;
I wanted the combined output of:
[1, 2, 3, 4, 5, 6]
var list = [].. addAll(list1).. addAll(list2); Dart now supports the concatenation of lists using the + operator.
A nested list is a list of lists or we can say the multidimensional list. Dart does not provide any native way to create a nested list. To create nested list we use the List. filled( ) or List.
List<SomeClass> list = list to search; List<String> matchingList = list of strings that you want to match against; list. where((item) => matchingList. contains(item. relevantProperty));
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);
Dart now supports concatenation of lists using the +
operator.
Example:
List<int> result = [0, 1, 2] + [3, 4, 5];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With