getUnionCodec function
Returns a codec for encoding and decoding union types.
This codec serializes and deserializes union values by selecting the correct variant based on the provided index functions.
Implementation
Codec<Object?, Object?> getUnionCodec(
List<Codec<Object?, Object?>> variants,
int Function(Object? value) getIndexFromValue,
int Function(Uint8List bytes, int offset) getIndexFromBytes,
) {
return combineCodec(
getUnionEncoder(variants.map(encoderFromCodec).toList(), getIndexFromValue),
getUnionDecoder(variants.map(decoderFromCodec).toList(), getIndexFromBytes),
);
}