getUnionCodec function

Codec<Object?, Object?> getUnionCodec(
  1. List<Codec<Object?, Object?>> variants,
  2. int getIndexFromValue(
    1. Object? value
    ),
  3. int getIndexFromBytes(
    1. Uint8List bytes,
    2. int offset
    )
)

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),
  );
}