addAll 是合并两个列表最常用的方式。
但要连接列表列表,您可以使用以下三个函数中的任何一个(示例如下):
-
expand - 将 Iterable 的每个元素扩展为零个或多个元素,
-
fold - 通过迭代地将集合中的每个元素与现有值组合,将集合缩减为单个值,
-
reduce - 通过使用提供的函数迭代组合集合的元素,将集合缩减为单个值。
void main() {
List<int> a = [1,2,3];
List<int> b = [4,5];
List<int> c = [6];
List<List<int>> abc = [a,b,c]; // list of lists: [ [1,2,3], [4,5], [6] ]
List<int> ints = abc.expand((x) => x).toList();
List<int> ints2 = abc.reduce((list1,list2) => list1 + list2);
List<int> ints3 = abc.fold([], (prev, curr) => prev + curr); // initial value is []
print(ints); // [1,2,3,4,5,6]
print(ints2); // [1,2,3,4,5,6]
print(ints3); // [1,2,3,4,5,6]
}