Skip to content

Dart enum toString is too verbose #30021

Closed
@jacob314

Description

@jacob314

Flutter can't use the default toString() for enum types as it is too verbose and instead has to carefully call a describeEnum(entry) helper whenever an enum entry is displayed in a message.
The describeEnum helper strips out the enum type from the result of calling entry.toString().
It would be nice if the default toString() for enum just displayed the enum value or if the enum class provided a toStringShort() method that returned just the enum value.

Note that you someone who really wanted to display Type.entry could just write

'${entry.runtimeType}.${entry}'

Example of current behavior:

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

main() {
  assert(Day.monday.toString() == 'Day.monday');
  assert(describeEnum(Day.monday) == 'monday');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-duplicateClosed in favor of an existing reportcore-mcustomer-fluttertype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions