If you come from Javascript, like me, you were probably expecting that when mapping over an array you had access to the index of the item, like so:
myArray.map((item, index) => {
// whatever
}
Well, the index in Dart is not available. So you will first have to convert the array to a map, with key-value pairs, like so:
myArray.asMap().entries.map((entry) {
int idx = entry.key; // this is the index
String val = entry.value;
return whatever;
}
Don't forget to call asMap
, like so: asMap()
, and chain entries
. If you need the outcome to be a Flutter List
, you need to add the toList()
at the end.