Dart How to get the “value” of an enum

后端 未结 15 2048
野的像风
野的像风 2020-12-29 00:50

Before enums were available in Dart I wrote some cumbersome and hard to maintain code to simulate enums and now want to simplify it. I need to get the value of the enum as

15条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 01:30

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

提交回复
热议问题