16.8 C
New York
Tuesday, September 27, 2022

How to Flatten a Nested List in Dart

In Dart, you can use the expand() method is to look for nested collections inside your collection and flatten them into a single list.

Example:

// https://www.kindacode.com
void main() {
  final _matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
  ];

  final _linear = _matrix.expand((element) => element).toList();
  print(_linear);
}

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.

Advertisements

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles