decode method
Decodes a data to a NotificationType.
If allowNull is true and the data cannot be decoded successfully,
then null is returned. However, if allowNull is false and the data
cannot be decoded successfully, then an UnimplementedError is thrown.
The allowNull is very handy when an API changes and a new enum value is added or removed,
and users are still using an old app with the old code.
Implementation
NotificationType? decode(dynamic data, {bool allowNull = true}) {
if (data != null) {
switch (data) {
case 0: return NotificationType.number0;
case 1: return NotificationType.number1;
case 2: return NotificationType.number2;
case 3: return NotificationType.number3;
case 4: return NotificationType.number4;
case 5: return NotificationType.number5;
case 6: return NotificationType.number6;
case 7: return NotificationType.number7;
case 8: return NotificationType.number8;
case 81: return NotificationType.number81;
case 82: return NotificationType.number82;
case 9: return NotificationType.number9;
case 10: return NotificationType.number10;
default:
if (!allowNull) {
throw ArgumentError('Unknown enum value to decode: $data');
}
}
}
return null;
}