Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created March 24, 2020 06:32
Show Gist options
  • Save CaiJingLong/57d2b5da3b562f80d49f44d99f6959a1 to your computer and use it in GitHub Desktop.
Save CaiJingLong/57d2b5da3b562f80d49f44d99f6959a1 to your computer and use it in GitHub Desktop.
extension method in dart
typedef void ForEachHandle<T>(int index, T item);
extension _A<T> on List<T> {
void forEachIndexed(void func(int index, T item)) {
for (var i = 0; i < this.length; i++) {
func(i, this[i]);
}
}
void forEachIndexed2(void Function(int index, T item) func) {
for (var i = 0; i < this.length; i++) {
func(i, this[i]);
}
}
void forEachIndexed3(ForEachHandle<T> func) {
for (var i = 0; i < this.length; i++) {
func(i, this[i]);
}
}
}
void main() {
[1, 2, 3].forEachIndexed((i, item) {
print("index:$i , value: $item");
});
[1, 2, 3].forEachIndexed2((i, item) {
print("index:$i , value: $item");
});
[1, 2, 3].forEachIndexed3((i, item) {
print("index:$i , value: $item");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment