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
// main.dart
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:
- Flutter & Dart: Displaying Large Numbers with Digit Grouping
- Using Static Methods in Dart and Flutter
- Dart: How to Update a Map
- Dart & Flutter: 2 Ways to Count Words in a String
- Conditional (Ternary) Operator in Dart and Flutter
- Using GetX to make GET/POST requests in Flutter
You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.