renderListData method

Future<String> renderListData({
  1. required List<Map<String, Object?>> data,
  2. int status = 200,
})

Renders a list of data as JSON and sends it in the response.

Sets the HTTP status code and content type for the response. Encodes the provided data into a JSON string and writes it to the response. Handles errors and sets the status code to 502 in case of failure.

data - A list of maps containing the data to be rendered as JSON. status - The HTTP status code to be set for the response. Default is 200.

Returns a Future<String> containing the JSON string of the rendered data.

Implementation

Future<String> renderListData({
  required List<Map<String, Object?>> data,
  int status = 200,
}) async {
  try {
    try {
      response.statusCode = status;
      response.headers.contentType = ContentType.json;
    } catch (e) {
      Console.i(e);
    }

    var renderString = WaJson.jsonEncoder(data, rq: this);
    await writeAndClose(renderString);
    return renderString;
  } catch (e) {
    try {
      response.statusCode = 502;
      response.headers.contentType = ContentType.json;
    } catch (e) {
      Console.i(e);
    }

    return jsonEncode({
      'status': 502,
      'error': e,
    });
  }
}