Returns decoded data, decoding it if not already decoded.
This is the raw access method to a request 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, this instance's contents are read in full and decoded according to the content-type of its request. 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 of the request is empty, this method will return null and no decoding is attempted.
The return type of this method depends on the codec selected from HTTPCodecRepository, determined by the content-type of the request.
If there is no codec in HTTPCodecRepository for the content type of the
request body being decoded, this method returns the unaltered 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
of this method is a List<String>
. The entire decoded request body is obtained by concatenating
each element of this list. It is preferable to use decodeAsString which automatically does this concatenation.
For application/json
and application/x-www-form-urlencoded
data, the return value is a List<Map>
that contains
exactly one object - the decoded JSON or form object as a Map. Prefer to use decodeAsMap, which returns
the single object from this list. Note that if the request body is a JSON list, the return value of this type
is List<List<Map<String, dynamic>>>
, where the outer list contains exactly one object: the decoded JSON list.
For custom codecs, the return type of this method is determined by the output of that codec. Note that
the reason String
data must be concatenated is that body data may be chunked and each chunk is decoded independently.
Whereas a JSON or form data must be read in full before the conversion is complete and so its codec only emits a single,
complete object.
Source
Future<List<dynamic>> get decodedData async { // Note that gzip decompression will automatically be applied by dart:io. if (!hasBeenDecoded) { if (_decodedData == null) { if (_request.headers.contentType != null) { var codec = HTTPCodecRepository.defaultInstance .codecForContentType(_request.headers.contentType); if (codec != null) { Stream<List<int>> stream = _request; if (retainOriginalBytes) { _bytes = await _readBytes(_request); stream = new Stream.fromIterable([_bytes]); } var bodyStream = codec.decoder.bind(stream).handleError((err) { throw new HTTPBodyDecoderException("Failed to decode request body.", underlyingException: err); }); _decodedData = await bodyStream.toList(); } else { _decodedData = await _readBytes(_request); } } else { _decodedData = await _readBytes(_request); } } } return _decodedData; }