BarcodeItems.csv constructor

BarcodeItems.csv(
  1. String text, {
  2. int column = 0,
  3. bool header = false,
  4. String? separator,
  5. bool unique = false,
})

The cells of one column (0 is the first) of the CSV text: one item a row, trimmed of spaces. Empty cells and rows without that column are left out. With header the first row is the column names, not an item; with unique an item that came before is left out.

The text is read as RFC 4180 says: a cell in double quotes may hold the separator, line breaks and quotes (written twice: ""). Rows end with \n, \r\n or \r; a byte order mark at the start is skipped.

separator is one character. null (the default) takes the one the first row has most of among ,, ; and tab: Excel saves CSV with ; in many countries.

const String csv = 'Name;EAN\n"Tea; green";4006381333931\n';
BarcodeItems.csv(csv, column: 1, header: true); // [4006381333931]

Throws ArgumentError if column is negative or separator is not one character other than a quote or a line break.

Implementation

factory BarcodeItems.csv(
  String text, {
  int column = 0,
  bool header = false,
  String? separator,
  bool unique = false,
}) {
  if (column < 0) {
    throw ArgumentError.value(column, 'column', 'cannot be negative');
  }
  final Iterable<List<String>> rows = csvRows(text, separator: separator);
  return BarcodeItems._(
    _collect(<String>[
      for (final List<String> row in header ? rows.skip(1) : rows)
        if (column < row.length) row[column],
    ], unique: unique),
  );
}