Returns decoded data, decoding it if not already decoded.
This is the raw access method to an HTTP body's decoded data. It is preferable to use methods such as decodeAsMap, decodeAsList, and decodeAsString, all of which invoke this method.
The first time this method is invoked, bytes is read in full and decoded according to contentType. The decoded data is stored in this instance so that subsequent access will return the cached decoded data instead of decoding it again.
If the body is empty, this method will return null and no decoding is attempted.
The elements of the return value depend on the codec selected from HTTPCodecRepository, determined by contentType. There are effectively three different scenarios:
If there is no codec in HTTPCodecRepository for the content type of the
request body being decoded, this method returns the flattened list of bytes directly
from the request body as List<int>
.
If the selected codec produces String
data (for example, any text
content-type), the return value
is a list of strings that, when concatenated, are the full String
body. It is preferable to use
decodeAsString which automatically does this concatenation.
For most contentTypes, the return value is a single element List
containing the decoded body object. For example,
this method return a List
with a single Map
when the body is a JSON object. If the body is a list of JSON objects,
this method returns a List
with a single List
element that contains the JSON objects. It is preferable to use
decodeAsMap or decodeAsList which unboxes the outer List
returned by this method.
Source
Future<List<dynamic>> get decodedData async { if (!hasBeenDecoded) { if (_decodedData == null) { if (contentType != null) { var codec = HTTPCodecRepository.defaultInstance .codecForContentType(contentType); if (codec != null) { Stream<List<int>> stream = bytes; if (retainOriginalBytes) { _bytes = await _readBytes(bytes); stream = new Stream.fromIterable([_bytes]); } var bodyStream = codec.decoder.bind(stream).handleError((err) { if (err is HTTPBodyDecoderException) { throw err; } throw new HTTPBodyDecoderException("Failed to decode request body.", underlyingException: err); }); _decodedData = await bodyStream.toList(); } else { _decodedData = await _readBytes(bytes); } } else { _decodedData = await _readBytes(bytes); } } } return _decodedData; }