Enum from String

前端 未结 18 2067
温柔的废话
温柔的废话 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:43

    This is all so complicated I made a simple library that gets the job done:

    https://pub.dev/packages/enum_to_string

    import 'package:enum_to_string:enum_to_string.dart';
    
    enum TestEnum { testValue1 };
    
    convert(){
        String result = EnumToString.parse(TestEnum.testValue1);
        //result = 'testValue1'
    
        String resultCamelCase = EnumToString.parseCamelCase(TestEnum.testValue1);
        //result = 'Test Value 1'
    
        final result = EnumToString.fromString(TestEnum.values, "testValue1");
        // TestEnum.testValue1
    }
    

提交回复
热议问题