dart map获取index
只需要index
list.asMap().keys.map((index) {
return something;
}
List<String> list = ['a', 'b', 'c', 'd'];
print(list.asMap()); // {0: a, 1: b, 2: c, 3: d}
print(list.asMap().keys); // (0, 1, 2, 3)
print(list.asMap().keys.map((index) => list[index]).toList()); // [a, b, c, d]
同时访问value和index
list.asMap().entries.map((entry) {
int index = entry.key;
String value = entry.value;
return something;
}
List list = ['a', 'b', 'c', 'd'];
List ll = list.asMap().entries.map((entry) {
int index = entry.key;
String value = entry.value;
return value + index.toString();
}).toList();
print(ll); // [a0, b1, c2, d3]