decodeIntegers method

List<List<int>> decodeIntegers(
  1. String input,
  2. CsvConfig config, {
  3. int? emptyAs,
})

Decode all fields as integers.

Throws CsvParseException (with row and column) for any field that is not a valid integer. Empty fields throw too, unless emptyAs provides an explicit fill value.

Implementation

List<List<int>> decodeIntegers(
  String input,
  CsvConfig config, {
  int? emptyAs,
}) {
  final stringRows = decodeStrings(input, config);
  final result = <List<int>>[];
  for (var r = 0; r < stringRows.length; r++) {
    final row = stringRows[r];
    final out = <int>[];
    for (var c = 0; c < row.length; c++) {
      final s = row[c];
      if (s.isEmpty) {
        if (emptyAs == null) {
          throw CsvParseException(
            'Empty field is not a valid integer '
            '(pass emptyAs to fill empty fields)',
            row: r,
            column: c,
          );
        }
        out.add(emptyAs);
      } else {
        final v = int.tryParse(s);
        if (v == null) {
          throw CsvParseException(
            'Field "$s" is not a valid integer',
            row: r,
            column: c,
          );
        }
        out.add(v);
      }
    }
    result.add(out);
  }
  return result;
}