Home > other >  How to find position in for loop in flutter?
How to find position in for loop in flutter?

Time:04-04

for (var i in options) Text(i);

This is my for loop, I want to know the position of i How to find that position. I used that for loop in Flutter Widget.

I need the solution for use the for loop inside the widgets I want to know the position of i

CodePudding user response:

You can use it like this:

for (int index = 0; i<options.length; i  ) {
  var option = options[index];
  Text(option);
}

Edit: Maybe a outer index variable will do.

int index = 0;
for (var i in options) {
  Text(i);
  index  ;
}

CodePudding user response:

options.indexOf(i) will return the index of the element

  • Related