I have a big enum data like
enum LargeCategoryEnum{
numberOne, numberTwo ;
factory LargeCategoryEnum.fromJson(String json) =>
values.byName(json);
}
and I want to get this enum from a json response but the problem is that the enum in the json response can be all lowercase or all uppercase. for example it can be NumberOne.
So in the LargeCategoryEnum.fromJson
it can not map it to any enum name and fails. how can I parse this json?
3
Answers
Try the following snippet:
which prints:
Switch over the lowercase would be good for small number of values in an enumerated type, while the values number increases you can use the following:
which prints:
Test.values gives you all the values, so you can compare your string with item.name:
My dartbag package has a
ParseEnum
extension that would let you do: