Enum from String

前端 未结 18 2068
温柔的废话
温柔的废话 2020-12-15 14:54

I have an Enum and a function to create it from a String because i couldn\'t find a built in way to do it



        
18条回答
  •  旧时难觅i
    2020-12-15 15:23

    There are a couple of enums packages which allowed me to get just the enum string rather than the type.value string (Apple, not Fruit.Apple).

    https://pub.dartlang.org/packages/built_value (this is more up to date)

    https://pub.dartlang.org/packages/enums

    void main() {
      print(MyEnum.nr1.index);            // prints 0
      print(MyEnum.nr1.toString());       // prints nr1
      print(MyEnum.valueOf("nr1").index); // prints 0
      print(MyEnum.values[1].toString())  // prints nr2
      print(MyEnum.values.last.index)     // prints 2
      print(MyEnum.values.last.myValue);  // prints 15
    }  
    

提交回复
热议问题