when<R> method

R when<R>(
  1. Map<Enum, R Function()> map
)

Calls the function corresponding to this enum value in map and returns the result.

Throws ArgumentError if this enum value is not in map.

enum Status { active, inactive, pending }

final label = status.when({
  Status.active: () => 'Active',
  Status.inactive: () => 'Inactive',
  Status.pending: () => 'Pending',
});

Implementation

R when<R>(Map<Enum, R Function()> map) {
  final fn = map[this];
  if (fn == null) throw ArgumentError('No case for enum value: $this');
  return fn();
}