enum Key { key1('key1'), key2('key2'); final String value; const Key(this.value); T mapFor({ required T Function() key1, required T Function() key2, }) { if (value == Key.key1.value) { return key1(); } else if (value == Key.key2.value) { return key2(); } throw Exception('Unexpected value for `value` of "$value" - did you forget to add a handler here?'); } } void main() { Key key = Key.key1; key.mapFor( key1: () => print('This was key1'), key2: () => print('This was key2'), ); }