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.

The type returned from decodeEnvelope is dynamic (not Object?), which means no type checking is performed on its return value. It is strongly recommended that the return value be immediately cast to a known type to prevent runtime errors due to typos that the type checker could otherwise catch.

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 Object? errorCode = messageCodec.readValue(buffer);
  final Object? errorMessage = messageCodec.readValue(buffer);
  final Object? 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');
}