csv_plus v1.2.0
pub.dev GitHub

Type inference

Values come back as the type they look like, with the cases that usually corrupt data guarded against.

Install#

dart pub add csv_plus
import 'package:csv_plus/csv_plus.dart';

What inference does#

final rows = CsvCodec().decode('name,age,score,active\nAlice,30,95.5,true');

rows[1]; // [Alice, 30, 95.5, true]
         //  String, int, double, bool

The guard that matters#

Naive inference destroys data. A zero-padded id, a phone number, a postcode: all look numeric and none of them are. Converting 007 to 7 loses information that cannot be recovered.

CsvCodec().decode('id,qty\n007,3');
// [[id, qty], ['007', 3]]
//              ^ still a String. 3 became an int.

Turning it off#

final codec = CsvCodec(CsvConfig(dynamicTyping: false)); // every field stays a String

Forcing a whole grid to one type#

These throw on a bad cell rather than inventing a value. Pass emptyAs to decide what a blank becomes.

codec.decodeStrings(csv);            // List<List<String>>
codec.decodeIntegers('1,2\n3,4');    // List<List<int>>
codec.decodeDoubles('1.5,2.5');      // List<List<double>>
codec.decodeBooleans('true,0');      // List<List<bool>>  (true/false/1/0)

Per-column types#

Whole-grid decoders are blunt. When columns differ, declare them with a schema instead.