Just like passing class type as argument to method I want to pass enum, as I want to write a general method which operates on enum.
I tried:
void foo(E):
print(E.values[0])
but it doesn't work.
Is there a way?
Enums are still restricted, you cannot implement the interface other than by creating an enum .
In the full example, which you can see in CodePen above, the enum is extended with four methods: displayTitle() , displayColorChangeText() , color() , and getRandomSelectedColor() .
This might work for you
typedef EnumValues<T> = List<T> Function();
void main() {
foo<E1>(() => E1.values);
foo<E2>(() => E2.values);
}
enum E1 { a, b }
enum E2 { c, d }
void foo<T>(EnumValues<T> valuesFn) {
var values = valuesFn();
for (var v in values) {
print(v);
}
}
See comments - shorter version
void main() {
foo<E1>(E1.values);
foo<E2>(E2.values);
}
enum E1 { a, b }
enum E2 { c, d }
void foo<T>(List<T> values) {
for (var v in values) {
print(v);
}
print(values[0]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With