Skip to content

Instantly share code, notes, and snippets.

@milomai
Last active December 30, 2020 09:37
Show Gist options
  • Select an option

  • Save milomai/c0f0387030c885457bb334ddfcda2154 to your computer and use it in GitHub Desktop.

Select an option

Save milomai/c0f0387030c885457bb334ddfcda2154 to your computer and use it in GitHub Desktop.
[Dart] Map list with index
List<int> list = [1, 2, 3];
List<String> result = list.asMap().entries.map((entry) {
int idx = entry.key;
int val = entry.value;
return "$idx: $val";
}).toList();
// or
List<String> result = list
.asMap()
.map((index, num) => MapEntry(index, "$index: $num"))
.values
.toList();
@milomai
Copy link
Author

milomai commented Nov 19, 2020

@milomai
Copy link
Author

milomai commented Dec 16, 2020

asMap() will keep the outter map's keys in numerical order, so the values will also follow this order.
Don't worry about the returnd list order.
ref: https://api.dart.dev/stable/dart-core/List/asMap.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment