In dart there any equivalent to the common:
enumerate(List) -> Iterator((index, value) => f)
or
List.enumerate() -> Iterator((index, value) => f)
or
List.map() -> Iterator((index, value) => f)
It seems that this is the easiest way but it still seems strange that this functionality wouldn't exist.
Iterable<int>.generate(list.length).forEach( (index) => {
newList.add(list[index], index)
});
Edit:
Thanks to @hemanth-raj i was able to find the solution I was looking for. I'll put this here for anyone who needs to do something similar.
List<Widget> _buildWidgets(List<Object> list) {
return list
.asMap()
.map((index, value) =>
MapEntry(index, _buildWidget(index, value)))
.values
.toList();
}
Alternatively you could create a synchronous generator function to return a iterable
Iterable<MapEntry<int, T>> enumerate<T>(Iterable<T> items) sync* {
int index = 0;
for (T item in items) {
yield MapEntry(index, item);
index = index + 1;
}
}
//and use it like this.
var list = enumerate([0,1,3]).map((entry) => Text("index: ${entry.key}, value: ${entry.value}"));
Map#forEach
? is it what you want? – pskinkList
? what do you mean? the docs say: "Applies f to each key/value pair of the map. Calling f must not add or remove keys from the map." – pskink