In Dart, you can iterate over a two-dimensional list using nested for loops. The outer loop iterates over rows, and the inner loop iterates over elements in each row, allowing you to access and process each element in the 2D list.
In this example,
void main() {
List<List<int>> matrix = [
[1, 2, 3],
[4, 5, 6]
];
for (var row in matrix) {
for (var val in row) {
print(val);
}
}
}
1 2 3 4 5 6
In this example,
void main() {
List<List<String>> matrix = [
['apple', 'banana'],
['cherry', 'orange']
];
for (var row in matrix) {
for (var str in row) {
print(str);
}
}
}
apple banana cherry orange
In this tutorial, we learned How to Iterate over a Two Dimensional List in Dart language with well detailed examples.