Home > Enterprise >  "for-in" statement syntax with multiple elements syntax issues , The same syntax works in
"for-in" statement syntax with multiple elements syntax issues , The same syntax works in

Time:09-17

var pairs = [
  [1, 2],
  [2, 2],
  [2, 3],
  [4, 4]
];

count4(x, y, pairs) {
  var same_count = 0;
  for (final x,final y in pairs) {
      if (x == y) {
        same_count  = 1;
      }
    }
  return same_count;
}

This code with similar syntax works in python, but i am trying to implement it in dart and running into all sorts of errors. Any ideas how i can re-write the syntax to make it work?

CodePudding user response:

for (final x,final y in pairs) is destructuring every element in the list and this is not supported in dart yet.

So you need to rewrite it like following:

count4(x, y, pairs) {
  var same_count = 0;
  for (final pair in pairs) {
      if (pair[0] == pair[1]) {
        same_count  = 1;
      }
    }
  return same_count;
}

By the way, you apparently don't need x,y as parameters if you don't use them.

  • Related