writeField method Null safety

void writeField(
  1. int fieldNumber,
  2. int fieldType,
  3. dynamic fieldValue
)

Implementation

void writeField(int fieldNumber, int fieldType, fieldValue) {
  final valueType = fieldType & ~0x07;

  if ((fieldType & PbFieldType._PACKED_BIT) != 0) {
    if (!fieldValue.isEmpty) {
      _writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
      final mark = _startLengthDelimited();
      for (var value in fieldValue) {
        _writeValueAs(valueType, value);
      }
      _endLengthDelimited(mark);
    }
    return;
  }

  final wireFormat = _wireTypes[_valueTypeIndex(valueType)];

  if ((fieldType & PbFieldType._MAP_BIT) != 0) {
    final keyWireFormat =
        _wireTypes[_valueTypeIndex(fieldValue.keyFieldType)];
    final valueWireFormat =
        _wireTypes[_valueTypeIndex(fieldValue.valueFieldType)];

    fieldValue.forEach((key, value) {
      _writeTag(fieldNumber, WIRETYPE_LENGTH_DELIMITED);
      final mark = _startLengthDelimited();
      _writeValue(
          PbMap._keyFieldNumber, fieldValue.keyFieldType, key, keyWireFormat);
      _writeValue(PbMap._valueFieldNumber, fieldValue.valueFieldType, value,
          valueWireFormat);
      _endLengthDelimited(mark);
    });
    return;
  }

  if ((fieldType & PbFieldType._REPEATED_BIT) != 0) {
    for (var i = 0; i < fieldValue.length; i++) {
      _writeValue(fieldNumber, valueType, fieldValue[i], wireFormat);
    }
    return;
  }
  _writeValue(fieldNumber, valueType, fieldValue, wireFormat);
}