CsvWriter class

CsvWriter wraps around a StringSink and enables writing data in CSV format. The CsvWriter maintains an internal structure for the current data record. Individual values can be set or read via accessors or via the get and set methods, accessed by header label (provided the CsvWriter was created with CsvWriter.withHeaders) and/or index. The data is written to the underlying sink record per record.

Example:

var csv = CsvWriter.withHeaders(mySink, ['Header #1', 'Header #2']);`
csv['Header #1'] = 'value';
csv.writeData();
await csv.close(); // recommended when mySink is a ClosableStringSink or an IOSink

By default, the internal data record values are reset to null after each call to writeData. This behavior can be overriden by calling writeData with clear set to false.

Constructors

CsvWriter(StringSink sink, int columns, {String separator = _defSeparator, String endOfLine = _defEndOfLine})
Builds a new CsvWriter bound to sink with CSV records consisting of columns values. separator (default is ',') and endOfLine (default is '\r\n') can be overriden. Using this constructor, data can only be set/read by index.
CsvWriter.withHeaders(StringSink sink, Iterable<String> headers, {String separator = _defSeparator, String endOfLine = _defEndOfLine})
Builds a new CsvWriter bound to sink. The supplied headers will be added as the first line, and CS records will consist of headers.length values. separator (default is ',') and endOfLine (default is '\r\n') can be overriden. Using this constructor, data may be set/read by header name and/or index.

Properties

columnCount int
Number of values per record.
no setter
done Future
Returns a Future that completes when this instance is closed. The returned Future is the same as the one returned by close.
no setter
endOfLine String
End-of-line character; default is '\r\n'.
final
hashCode int
The hash code for this object.
no setterinherited
hasHeader bool
true if this instance was constructed with CsvWriter.withHeaders.
final
rowCount int
Count of records that have been written to the underlying StringSink. This count excludes the header line if hasHeader is true.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
separator String
Separator character; default is ','.
final

Methods

clearData() → void
Clears the current record.
close() Future
If the underlying StringSink is an IOSink, flushes and closes it. If it is a ClosableStringSink, simply close it. This method returns the same future as done. Multiple calls to close are allowed but only the first one will have an effect.
flush() Future
Flushes the underlying StringSink if it is an IOSink, otherwise returns a completed Future.
get({String header = '', int index = -1}) → dynamic
Gets value for header / index from the current record. The column index in the CSV record is retrieved according to header and index. If header is not set, index is used as the column index (starting from 0). If header is set, the column index will be retrieved from the set of headers provided to CsvWriter.withHeaders. If multiple headers have the same label, index can be used to distinguish amongst them (starting from 0). If no match is found, or if index is out of bounds, throws an InvalidHeaderException.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
set(dynamic value, {String header = '', int index = -1}) → void
Sets value for header / index in the current record. The column index in the CSV record is retrieved according to header and index. If header is not set, index is used as the column index (starting from 0). If header is set, the column index will be retrieved from the set of headers provided to CsvWriter.withHeaders. If multiple headers have the same label, index can be used to distinguish amongst them (starting from 0). If no match is found, or if index is out of bounds, throws an InvalidHeaderException. Data will not be written to the StringSink before writeData is called.
setData(dynamic data) → void
Loads data into the current data structure. data can either be a List (data is loaded by index) or a Map<String, dynamic> (data is loaded by header name).
toString() String
A string representation of this object.
inherited
writeData({dynamic data, bool clear = true}) → void
Writes the current record to the CSV file, if is not empty. The current record is considered empty if all of its values are null or their String representations are empty or contain only whitespaces. If data is provided, it is passed to setData to populate the internal data record before the write operation occurs. After the record has been written to the underlying StringSink, the count of records is incremented and the internal data record is reset unless clear is false.

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](dynamic header) → dynamic
Gets value for header from the current record. If header is an int, it is interpreted as the column index. If header is a String, it is used to lookup the header and find the column index.
operator []=(dynamic header, dynamic value) → void
Sets value for header in the current record. If header is an int, it is interpreted as the column index. If header is a String, it is used to lookup the header and find the column index. Data will not be written to the StringSink before writeData is called.