Create by https://gist.github.com/CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0
Created
March 24, 2020 06:32
-
-
Save CaiJingLong/57d2b5da3b562f80d49f44d99f6959a1 to your computer and use it in GitHub Desktop.
extension method in dart
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
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