describeEnum function Null safety

String describeEnum(
  1. Object enumEntry

Returns a short description of an enum value.

Strips off the enum class name from the enumEntry.toString().

For real enums, this is redundant with calling the name getter on the enum value (see, a feature that was added to Dart 2.15.

This function can also be used with classes whose toString return a value in the same form as an enum (the class name, a dot, then the value name). For example, it's used with SemanticsAction, which is written to appear to be an enum but is actually a bespoke class so that the index values can be set as powers of two instead of as sequential integers.

enum Day {
  monday, tuesday, wednesday, thursday, friday, saturday, sunday

void validateDescribeEnum() {
  assert(Day.monday.toString() == 'Day.monday');
  assert(describeEnum(Day.monday) == 'monday');
  assert( == 'monday'); // preferred for real enums


String describeEnum(Object enumEntry) {
  if (enumEntry is Enum)
  final String description = enumEntry.toString();
  final int indexOfDot = description.indexOf('.');
    indexOfDot != -1 && indexOfDot < description.length - 1,
    'The provided object "$enumEntry" is not an enum.',
  return description.substring(indexOfDot + 1);