decodeEnvelope method
- 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) {
final Object? decoded = const JSONMessageCodec().decodeMessage(envelope);
if (decoded is! List) {
throw FormatException('Expected envelope List, got $decoded');
}
if (decoded.length == 1) {
return decoded[0];
}
if (decoded.length == 3
&& decoded[0] is String
&& (decoded[1] == null || decoded[1] is String)) {
throw PlatformException(
code: decoded[0] as String,
message: decoded[1] as String?,
details: decoded[2],
);
}
if (decoded.length == 4
&& decoded[0] is String
&& (decoded[1] == null || decoded[1] is String)
&& (decoded[3] == null || decoded[3] is String)) {
throw PlatformException(
code: decoded[0] as String,
message: decoded[1] as String?,
details: decoded[2],
stacktrace: decoded[3] as String?,
);
}
throw FormatException('Invalid envelope: $decoded');
}