writeCsvLine function
Encodes one row of fields as a single CSV line (no trailing newline).
A field is wrapped in double quotes when it contains the delimiter, a
double quote, a CR, or an LF — exactly the characters that would otherwise
corrupt the row structure (a delimiter splits the field, a bare quote
confuses the parser, a CR/LF injects a phantom row). Inner quotes are
doubled per RFC 4180. Set forceQuote to quote every field unconditionally,
which some downstream consumers require.
Example:
writeCsvLine(['a,b', 'c']); // '"a,b",c'
Audited: 2026-06-12 11:26 EDT
Implementation
String writeCsvLine(List<String> fields, {String delimiter = ',', bool forceQuote = false}) =>
fields.map((String f) => _encodeField(f, delimiter, forceQuote)).join(delimiter);