June 2017
Intermediate to advanced
873 pages
18h 9m
English
A list can contain other lists as elements. This is effectively a two-dimensional list or array. Flattening means making a single list with all sublist items contained in it. Take a look at the different possibilities in flatten_list.dart.
We show three ways to flatten a list in the following code:
List lst = [[1.5, 3.14, 45.3], ['m', 'pi', '7'], [true, false, true]];
// flattening lst must give the following resulting List flat:
// [1.5, 3.14, 45.3, m, pi, 7, true, false, true]
void main() {
// 1- using forEach and addAll:
var flat = [];
lst.forEach((e) => flat.addAll(e));
print(flat);
// 2- using Iterable.expand:
flat = lst.expand((i) => i).toList();
// 3- more nesting levels, work recursively: lst = [[1.5, ...Read now
Unlock full access