formatRows method

String formatRows(
  1. Iterable<Iterable<Object?>> rows
)

Formats the specified iterable of iterable of string rows as delimiter-separated values, returning a string.

This operation is the reverse of parseRows. Each row will be separated by a newline (\n), and each column within each row will be separated by the delimiter (such as a comma, ,). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.

To convert an iterable of maps to an iterable of iterables while explicitly specifying the columns, use Iterable.map. For example:

final string = csvFormatRows(data.map((d) {
  return [
    (d["year"] as DateTime).year, // Assuming d["year"] is a DateTime instance.
    d["make"],
    d["model"],
    d["length"]
  ];
}));

Alternatively, you can use a list of column names with List.followedBy to generate the first row:

final string = csvFormatRows([
  <Object>["year", "make", "model", "length"]
].followedBy(data.map((d) {
  return [
    (d["year"] as DateTime).year, // Assuming d["year"] is a DateTime instance.
    d["make"],
    d["model"],
    d["length"]
  ];
})));

Implementation

String formatRows(Iterable<Iterable<Object?>> rows) {
  return rows.map(formatRow).join("\n");
}