Body.fromDataStream constructor

Body.fromDataStream(
  1. Stream<Uint8List> body, {
  2. Encoding? encoding,
  3. MimeType? mimeType = MimeType.octetStream,
  4. Map<String, String> parameters = const {},
  5. int? contentLength,
})

Creates a body from a Stream of Uint8List.

This is useful for large files or streaming data where you don't want to load everything into memory at once.

Examples:

// Stream with known length (recommended for better HTTP performance)
final streamBody = Body.fromDataStream(
  fileStream,
  mimeType: MimeType.pdf,
  contentLength: fileSize,
);

// Stream with unknown length (uses chunked encoding)
final dynamicBody = Body.fromDataStream(
  dynamicStream,
  mimeType: MimeType.json,
  // contentLength omitted for chunked encoding
);

// Multipart stream with its boundary
final multipartBody = Body.fromDataStream(
  partsStream,
  mimeType: MimeType.multipartFormData,
  parameters: {'boundary': boundary},
);

parameters holds media type parameters other than charset, which encoding sets. Throws ArgumentError if parameters has a charset, or is not empty while mimeType is null.

Implementation

factory Body.fromDataStream(
  final Stream<Uint8List> body, {
  final Encoding? encoding,
  final MimeType? mimeType = MimeType.octetStream,
  final Map<String, String> parameters = const {},
  final int? contentLength,
}) {
  return Body._(
    body,
    contentLength,
    encoding: encoding ?? (mimeType?.isText == true ? utf8 : null),
    mimeType: mimeType,
    parameters: parameters,
  );
}