fromSerializedEnum function

dynamic fromSerializedEnum (int serialized, Type type, Function factory)

Converts the serialized integer value into an enum in dependence of the factory result

Example:

enum Color {
  red,
  blue
}

...

fromSerializedEnum(0, Color, () => Color.values);  // returns `Color.red`
fromSerializedEnum(1, Color, () => Color.values);  // returns `Color.blue`
fromSerializedEnum(null, Color, () => Color.values);  // returns `null`
fromSerializedEnum(Color.blue, Color, () => Color.values);  // returns `Color.blue`

Implementation

fromSerializedEnum(int serialized, Type type, Function factory) {
  return serialized == null || serialized.runtimeType == type ? serialized : factory()[serialized];
}