renderString method

Future<String> renderString({
  1. required String text,
  2. int status = 200,
  3. ContentType? contentType,
})

Renders a plain text response.

Sets the HTTP status code and content type for the response. Writes the provided text to the response and handles errors by logging them and returning a default error string.

text - The text to be rendered in the response. status - The HTTP status code to be set for the response. Default is 200. contentType - The content type to be set for the response. Defaults to ContentType.text if not provided.

Returns a Future<String> containing the rendered text.

Implementation

Future<String> renderString({
  required String text,
  int status = 200,
  ContentType? contentType,
}) async {
  try {
    try {
      response.statusCode = status;
      response.headers.contentType = contentType ?? ContentType.text;
    } catch (e) {
      renderError(502, params: {'error': e});
    }
    await writeAndClose(text);
    return text;
  } catch (e) {
    renderError(502, params: {'error': e});
    return 'console.log("$e");';
  }
}