Type switch case
void main() { | |
final Object something = TypeA(); | |
something.matchTypes({ | |
TypeA : () => print("found A"), | |
TypeB : () => print("found B"), | |
}); | |
} | |
extension Ext<ANY> on ANY { | |
void matchTypes<T>(Map<Object, void Function()> map) { | |
final key = map.keys.firstWhere((it) => it is T, orElse: () => null); | |
if(key == null) { | |
throw "No match found for type $T"; | |
} | |
map[key].call(); | |
} | |
} | |
class TypeA { | |
String name; | |
} | |
class TypeB { | |
String age; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment