How to read a CSV file in Dart
Load a file from disk into rows you can work with.
Install#
dart pub add csv_plusimport 'package:csv_plus/csv_plus.dart';Read a file#
File helpers live in a separate import, so the core library never pulls in dart:io and still works in the browser.
import 'package:csv_plus/io.dart';
void main() async {
final table = await CsvFile.read('data.csv');
print(table.rows.length);
print(table.rows.first['name']);
}Write and append#
await CsvFile.write('out.csv', table);
await CsvFile.append('out.csv', [['Zoe', 41]]);Reading a file you already hold as text#
If the content arrived over the network or from an asset rather than disk, decode it directly and skip the io import:
import 'package:csv_plus/csv_plus.dart';
final rows = CsvCodec().decode(responseBody);Large files#
Reading a whole file into memory is fine until it is not. For anything big, stream it instead, which holds one row at a time regardless of file size:
import 'package:csv_plus/io.dart';
await for (final row in CsvFile.stream('huge.csv')) {
process(row);
}See large files for the detail, including byte streams and backpressure.