Google is committed to advancing racial equity for Black communities. See how.

encode method

  • @override

void encode (Encoder encoder, T value, int offset) override

Implementation

@override
void encode(Encoder encoder, T value, int offset) {
  // Determining max index
  int maxIndex = -1;
  for (int i = 0; i < members.length; i++) {
    final field = value.$field(i);
    if (field != null) {
      maxIndex = i;
    }
  }
  int maxOrdinal = maxIndex + 1;

  // Header.
  encoder
    ..encodeUint64(maxOrdinal, offset)
    ..encodeUint64(kAllocPresent, offset + 8);

  // Sizing
  int envelopeOffset = encoder.alloc(maxOrdinal * _kEnvelopeSize);

  // Envelopes, and fields.
  for (int i = 0; i <= maxIndex; i++) {
    final field = value.$field(i);
    final fieldPresent = field != null;
    if (fieldPresent) {
      _encodeEnvelopePresent(encoder, envelopeOffset, field, members[i]);
    } else {
      _encodeEnvelopeAbsent(encoder, envelopeOffset);
    }
    envelopeOffset += _kEnvelopeSize;
  }
}