format method
Formats the specified iterable of map rows as delimiter-separated values, returning a string.
This operation is the inverse of parse. 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.
If columns
is not specified, the list of column names that forms the
header row is determined by the union of all properties on all objects in
rows; the order of columns is the order in which they appear. If columns
is specified, it is an iterable of strings representing the column names.
For example:
final string = csvFormat(data, ["year", "make", "model", "length"]);
All fields on each row map will be coerced to strings. If the field value is null, the empty string is used. If the field value is a Date, the ECMAScript date-time string format (a subset of ISO 8601) is used: for example, dates at UTC midnight are formatted as YYYY-MM-DD. For more control over which and how fields are formatted, first map rows to an iterable of iterable of string, and then use formatRows.
Implementation
String format(Iterable<Map<String, Object?>> rows,
[Iterable<String>? columns]) {
columns ??= _inferColumns(rows);
return [columns.map(formatValue).join(_delimiter)]
.followedBy(_preformatBody(rows, columns))
.join("\n");
}