Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart How to get the "value" of an enum

Tags:

enums

dart

People also ask

How do you find the value of an enum?

To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

Can enums have a value?

By default enums have their own string values, we can also assign some custom values to enums.

What is the value of enums?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


Dart 2.7 comes with new feature called Extension methods. Now you can write your own methods for Enum as simple as that!

enum Day { monday, tuesday }

extension ParseToString on Day {
  String toShortString() {
    return this.toString().split('.').last;
  }
}

main() {
  Day monday = Day.monday;
  print(monday.toShortString()); //prints 'monday'
}

Bit shorter:

String day = theDay.toString().split('.').last;

Sadly, you are correct that the toString method returns "day.MONDAY", and not the more useful "MONDAY". You can get the rest of the string as:

day theDay = day.MONDAY;      
print(theDay.toString().substring(theDay.toString().indexOf('.') + 1));

Hardly convenient, admittedly.

Another way to get the enum name as a string, one which is shorter, but also less efficient because it creates an unnecessary string for first part of the string too, is:

theDay.toString().split('.').last

If performance doesn't matter, that's probably what I'd write, just for brevity.

If you want to iterate all the values, you can do it using day.values:

for (day theDay in day.values) {
  print(theDay);
}

Simplest way to get the name of an enum is a standard method from the flutter/foundation.dart

describeEnum(enumObject)

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

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

enum Day {
  monday,
  tuesday,
}

Direct way:

var dayInString = describeEnum(Day.monday);
print(dayInString); // prints 'monday'

Using Extension:

extension DayEx on Day {
  String get inString => describeEnum(this);
}

You can use it like:

void main() {
  Day monday = Day.monday;
  String inString = monday.inString; // 'monday'
}

There is a more elegant solution:

enum SomeStatus {
  element1,
  element2,
  element3,
  element4,
}

const Map<SomeStatus, String> SomeStatusName = {
  SomeStatus.element1: "Element 1",
  SomeStatus.element2: "Element 2",
  SomeStatus.element3: "Element 3",
  SomeStatus.element4: "Element 4",
};

print(SomeStatusName[SomeStatus.element2]) // prints: "Element 2"

enum day {MONDAY, TUESDAY}
print( 'Today is ${describeEnum(day.MONDAY)}' );

console output: Today is MONDAY