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
sinkwith CSV records consisting ofcolumnsvalues.separator(default is',') andendOfLine(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 suppliedheaderswill be added as the first line, and CS records will consist ofheaders.lengthvalues.separator(default is',') andendOfLine(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
-
trueif 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/indexfrom the current record. The column index in the CSV record is retrieved according toheaderandindex. Ifheaderis not set,indexis used as the column index (starting from 0). Ifheaderis set, the column index will be retrieved from the set of headers provided to CsvWriter.withHeaders. If multiple headers have the same label,indexcan be used to distinguish amongst them (starting from 0). If no match is found, or ifindexis 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
valueforheader/indexin the current record. The column index in the CSV record is retrieved according toheaderandindex. Ifheaderis not set,indexis used as the column index (starting from 0). Ifheaderis set, the column index will be retrieved from the set of headers provided to CsvWriter.withHeaders. If multiple headers have the same label,indexcan be used to distinguish amongst them (starting from 0). If no match is found, or ifindexis out of bounds, throws an InvalidHeaderException. Data will not be written to the StringSink before writeData is called. -
setData(
dynamic data) → void -
Loads
datainto the current data structure.datacan 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
nullor their String representations are empty or contain only whitespaces. Ifdatais 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 unlessclearisfalse.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
dynamic header) → dynamic -
Gets value for
headerfrom the current record. Ifheaderis an int, it is interpreted as the column index. Ifheaderis a String, it is used to lookup the header and find the column index. -
operator []=(
dynamic header, dynamic value) → void -
Sets value for
headerin the current record. Ifheaderis an int, it is interpreted as the column index. Ifheaderis 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.