'dart modulo operator circular list

I want to use the modulo (%) operator in order to access to list items in a circular way. But the code below doesn't work:

void main() {
  List<String> myList = ['one', 'two', 'three', 'four'];
  int currIdx = 0;
  
  for (int i = 0;i < 10;i++) {
    print(myList[currIdx]);
    currIdx = currIdx++ % myList.length;
  }
}

enter image description here



Solution 1:[1]

Here's the solution: instead of coding

currIdx++

you must code

++currIdx

Complete code:

void main() {
  List<String> myList = ['one', 'two', 'three', 'four'];
  int currIdx = 0;
  
  for (int i = 0;i < 10;i++) {
    print(myList[currIdx]);
    currIdx = ++currIdx % myList.length; // look second comment below which is very useful !
  }
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1