Last active
November 14, 2020 04:51
-
-
Save channainfo/5900046b26eb93da96583f569990acba to your computer and use it in GitHub Desktop.
List map with index return List and Iterable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/extensions/list_map_with_index_extension.dart | |
extension MapWithIndex<T> on List<T> { | |
List<R> mapWithIndex<R>(R Function(T, int i) callback) { | |
List<R> result = []; | |
for (int i = 0; i < this.length; i++) { | |
R item = callback(this[i], i); | |
result.add(item); | |
} | |
return result; | |
} | |
Iterable<R> mapWithIndexIterable<R>(R Function(T, int i) callback) { | |
return this.asMap().keys.toList().map((index) => callback(this[index], index)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For ease of maintenance