parseWith<R> method

(List<R>, {List<String> columns}) parseWith<R>(
  1. String text,
  2. R? conversion(
    1. Map<String, String>,
    2. int,
    3. List<String>
    )
)

Similar to parse, but applies the specified conversion function to each row.

See autoType for a convenient row conversion function that infers and coerces common types like numbers and strings.

The specified conversion function is invoked for each row, being passed an map representing the current row (d), the index (i) starting at zero for the first non-header row, and the list of column names. If the returned value is null, the row is skipped and will be omitted from the list returned by parseWith; otherwise, the returned value defines the corresponding row map. For example:

final data = csvParseWith(string, (d, _, __) {
  return {
    "year": DateTime(int.parse(d["Year"]!), 0, 1), // lowercase and convert "Year" to Date
    "make": d["Make"], // lowercase
    "model": d["Model"], // lowercase
    "length": num.parse(d["Length"]!) // lowercase and convert "Length" to number
  };
});

Implementation

(List<R>, {List<String> columns}) parseWith<R>(String text,
    R? Function(Map<String, String>, int, List<String>) conversion) {
  R? Function(List<String>, int)? convert;
  var columns = <String>[],
      rows = parseRowsWith<R>(text, (row, i) {
        if (convert != null) return convert!(row, i - 1);
        columns = row;
        convert = _customConverter(row, conversion);
        return null;
      });
  return (rows, columns: columns);
}