decode method
Decodes a value of type T
using the decoder
.
The implementation should first use Decoder.whatsNext to determine the type of the encoded data.
Then it should use one of the Decoders .decode...()
methods to decode into its target type.
If the returned DecodingType is not supported, the implementation can use Decoder.expect to throw a detailed error.
Implementation
@override
DateTime decode(Decoder decoder) {
final decodingType = decoder.whatsNext();
final value = switch (decodingType) {
DecodingType<DateTime>() => decoder.decodeObject<DateTime>(),
_ => switch (preferredFormat) {
DateTimeFormat.auto => switch (decoder.whatsNext()) {
DecodingType.string => DateTime.parse(decoder.decodeString()),
DecodingType.int || DecodingType.num => DateTime.fromMillisecondsSinceEpoch(decoder.decodeInt()),
DecodingType.unknown when decoder.isHumanReadable() => DateTime.parse(decoder.decodeString()),
DecodingType.unknown => DateTime.fromMillisecondsSinceEpoch(decoder.decodeInt()),
_ => decoder.expect('string, int or custom date'),
},
DateTimeFormat.iso8601 => DateTime.parse(decoder.decodeString()),
DateTimeFormat.unixMilliseconds => DateTime.fromMillisecondsSinceEpoch(decoder.decodeInt()),
}
};
if (convertUtc) {
return value.toLocal();
}
return value;
}