Skip to content

Instantly share code, notes, and snippets.

@passsy

passsy/main.dart

Created Apr 23, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.