Skip to content

Instantly share code, notes, and snippets.

@marcusedu
Created October 2, 2019 17:50
Show Gist options
  • Save marcusedu/46e35566ef528916687aebaf1ec0bce6 to your computer and use it in GitHub Desktop.
Save marcusedu/46e35566ef528916687aebaf1ec0bce6 to your computer and use it in GitHub Desktop.
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