Enum from String

前端 未结 18 2033
温柔的废话
温柔的废话 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条回答
  •  温柔的废话
    2020-12-15 15:47

    Here is the function that converts given string to enum type:

    EnumType enumTypeFromString(String typeString) => EnumType.values
        .firstWhere((type) => type.toString() == "EnumType." + typeString);
    

    And here is how you convert given enum type to string:

    String enumTypeToString(EnumType type) => type.toString().split(".")[1];
    

提交回复
热议问题