CsvData class
CsvData holds the values from a CSV line. If the CsvLoader was created with CsvLoader.withHeaders, it also holds a reference to the list of headers found in the CSV's first non-empty row. Values can be accessed by index (absolute value), or (when created with CsvLoader.withHeaders) by header with relative index when necessary (i.e. when several headers have the same label).
Example:
var csv = CsvLoader.withHeaders(myStream);`
await for (CsvData row in csv.rows) {
// assuming the stream has headers: Label,Duplicate,Data,Duplicate
// by absolute index (0-based) --> first column
final firstValue = row[0];
// by header name --> since 'Label' is the first header, same as row[0]
final labelValue = row['Label'];
// by header name --> since 'Data' is the third header, same as row[2]
final dataValue = row['Data'];
// by header and relative index (0-based) --> same as row[0] again
final firstLabelValue = row.get('Label', 0);
// by header and relative index (0-based) --> would throw as there is only one header named 'Label'
// final secondLabelValue = row.get('Label', 1);
// by header and relative index (0-based) --> same as row[3]
final secondDuplicateValue = row.get('Duplicate', 1);
// by header name --> would throw as there is more than one header named 'Duplicate'
// final duplicateValue = row['Duplicate'];
// by header and relative index (0-based) --> would throw as there are only 2 headers named 'Duplicate'
// final thirdDuplicateValue = row.get('Duplicate', 2);
}
Properties
- columnCount → int
-
Count of columns according to the CSV headers; 0 if the reader was NOT constructed with
CsvLoader.withHeaders.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
headers
→ Iterable<
String> ? -
List of headers found in the first non-empty line of the CSV data source. This list is populated
if the reader was constructed with CsvLoader.withHeaders; otherwise it is empty.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
values
→ Iterable<
String> -
List of values. Note that
values.length
may be different from columnCount, e.g. if the reader was not constructed with CsvLoader.withHeaders, or the CSV row has more or less values than headers.no setter
Methods
-
get(
[String header = '', int index = -1]) → String? -
Gets value for
header
/index
from the current record. The column index in the CSV record is retrieved according toheader
andindex
. Ifheader
is not set,index
is used as the column index (starting from 0). Ifheader
is set, the column index will be retrieved from headers, provided the reader was constructed with CsvLoader.withHeaders. If several headers have the same label,index
can be used to distinguish amongst them (starting from 0). If no match is found, or ifindex
is out of bounds, throws an InvalidHeaderException. Returnsnull
if the CSV line has fewer values than headers. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
dynamic header) → String? -
Gets value for
header
from the current record. Ifheader
is an int, it is interpreted as the column index. Ifheader
is a String, it is used to lookup the header and find the column index.