render method

  1. @override
Future<void> render(
  1. Response response
)
override

Renders the response by writing headers and streaming the content.

This method first writes the content type and any additional headers to the response. If the content length is known, it is also written to the response headers. The headers are sent immediately before streaming the content. If an error occurs during streaming, the response status is set to 500 (Internal Server Error) and an error message is written to the response.

Implementation

@override
Future<void> render(Response response) async {
  writeContentType(response);

  if (contentLength != null && contentLength! >= 0) {
    response.headers.set('Content-Length', contentLength.toString());
  }

  if (headers != null) {
    _writeHeaders(response, headers!);
  }

  // Ensure headers are sent before writing the body
  response.writeHeaderNow();

  try {
    await response.addStream(reader);
  } catch (err) {
    // Handle any errors during streaming
    response.statusCode = HttpStatus.internalServerError;
    response.write('Internal Server Error');
  }
}