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

encode method

*<Null safety>*
  • @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.
  final unknownDataMap = value.$unknownData;
  for (int i = 0; i <= maxIndex; i++) {
    var field = value.$field(i);
    FidlType? fieldType;
    if (i < members.length) {
      fieldType = members[i];
    } else if (unknownDataMap != null) {
      final unknownData = unknownDataMap[i];
      if (unknownData != null) {
        _maybeThrowOnUnknownHandles(resource, unknownData);
        field = unknownData;
        fieldType = UnknownRawDataType(
            numBytes: unknownData.data.length,
            numHandles: unknownData.handles.length);
      }
    }

    if (field != null && fieldType != null) {
      _encodeEnvelopePresent(encoder, envelopeOffset, field, fieldType);
    } else {
      _encodeEnvelopeAbsent(encoder, envelopeOffset);
    }
    envelopeOffset += _kEnvelopeSize;
  }
}