I'm using the following code to have enum values and a corresponding string associated with it. I was wondering if there's a simpler way to accomplish this?

enum StyleType {
  casual,
  chic,
  urban,
  vintage,
  punk,
}

class Style{  
  final StyleType type;

  Style(this.type);  

  @override
  String toString() {
      switch (type) {
      case StyleType.casual:
        return 'Casual';
      case StyleType.chic:
        return 'Chic';
      case StyleType.punk:
        return 'Punk';
      case StyleType.urban:
        return 'Urban';
      case StyleType.vintage:
        return 'Vintage';

    }
  }  

}


Solution 1: tareq albeesh

Here's a simple way

void main() {

  print(test.a.toString().split(".")[1]);
  }

enum test {a,b}

this would output a


Solution 2: ManuH68

Since Dart 2.15 you can get the string by using .name property:

print(StyleType.casual.name); // => 'casual'