decodeEnvelope method Null safety

  1. @override
dynamic decodeEnvelope (
  1. ByteData envelope
)
override

Decodes the specified result envelope from binary.

Throws PlatformException, if envelope represents an error, otherwise returns the enveloped result.

Implementation

@override
dynamic decodeEnvelope(ByteData envelope) {
  // First byte is zero in success case, and non-zero otherwise.
  if (envelope.lengthInBytes == 0)
    throw const FormatException('Expected envelope, got nothing');
  final ReadBuffer buffer = ReadBuffer(envelope);
  if (buffer.getUint8() == 0)
    return messageCodec.readValue(buffer);
  final dynamic errorCode = messageCodec.readValue(buffer);
  final dynamic errorMessage = messageCodec.readValue(buffer);
  final dynamic errorDetails = messageCodec.readValue(buffer);
  final String? errorStacktrace = (buffer.hasRemaining) ? messageCodec.readValue(buffer) as String : null;
  if (errorCode is String && (errorMessage == null || errorMessage is String) && !buffer.hasRemaining)
    throw PlatformException(code: errorCode, message: errorMessage as String, details: errorDetails, stacktrace: errorStacktrace);
  else
    throw const FormatException('Invalid envelope');
}