2

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';

    }
  }  

}
Miguel Dey
  • 187
  • 1
  • 5

2 Answers2

6

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

print(StyleType.casual.name); // => 'casual'
ManuH68
  • 481
  • 2
  • 6
0

Here's a simple way

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

enum test {a,b}

this would output a

tareq albeesh
  • 1,701
  • 2
  • 10
  • 13