toCsv method

Future<void> toCsv(
  1. String path, {
  2. bool includeHeader = true,
  3. String? nullRepresentation = null,
  4. String fieldDelimiter = ',',
  5. String textDelimiter = '"',
  6. String eolToken = '\n',
  7. Encoding encoding = utf8,
})

Save the instance as csv to path.

Set includeHeader to false to only include the data in the csv. Null values will be saved as nullRepresentation. fieldDelimiter, textDelimiter and eolToken are forwarded to the underlying CSV encoder. The encoding specifies the encoding of the saved file.

Implementation

Future<void> toCsv(String path,
    {bool includeHeader = true,
    String? nullRepresentation = null,
    String fieldDelimiter = ',',
    String textDelimiter = '"',
    String eolToken = '\n',
    Encoding encoding = utf8}) {
  DataMatrix fields = this;

  // NOTE: this should be done by the Csv encoder
  if (nullRepresentation != null) {
    fields = fields
        .map((row) =>
            row.map((e) => e ?? nullRepresentation).toFixedLengthList())
        .toFixedLengthList();
  }

  final rows = includeHeader ? <List<Record>>[columnNames] + fields : fields;

  return File(path).writeAsString(
      CsvEncoder(
        fieldDelimiter: fieldDelimiter,
        lineDelimiter: eolToken,
        quoteCharacter: textDelimiter,
      ).convert(rows),
      encoding: encoding);
}