flatten method
Returns a new lazy Iterable where all elements of nested Iterables are flattened.
This method iterates over each element of the current iterable and flattens any nested Iterables by yielding their individual elements. The resulting iterable contains all elements in a flat structure.
-
Returns: A new lazy Iterable with flattened elements.
-
Examples:
Iterable<List<int>> nestedList = [[1, 2], [3, 4, 5], [6]]; Iterable<int> flattenedList = nestedList.flatten(); print(flattenedList); // [1, 2, 3, 4, 5, 6]
Note: This method does not modify the original iterable. It creates a new lazy iterable with the flattened elements, leaving the original iterable unchanged.
Implementation
Iterable<dynamic> flatten() sync* {
for (var current in this) {
yield* (current as Iterable);
}
}