Created
October 2, 2019 17:50
-
-
Save marcusedu/46e35566ef528916687aebaf1ec0bce6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum OrderType { asc, desc, sad } | |
String enumToString(dynamic enumValue) => | |
enumValue.toString().replaceAll(RegExp(r"^.*?\."), ""); | |
E stringToEnum<E>(List<E> values, String target, [E defaultIfNoMatch]) { | |
return values.firstWhere((enm) => enumToString(enm) == target, | |
orElse: () => defaultIfNoMatch); | |
} | |
void main() { | |
print("Enum To String: " + enumToString(OrderType.asc)); | |
print("String to Enum: " + | |
stringToEnum<OrderType>(OrderType.values, 'ASC', OrderType.sad).toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment