ml_dataset_loader
ml_dataset_loader is a lightweight Dart library for loading structured datasets from CSV files. It is designed with performance and flexibility in mind, offering support for type inference, quoted fields, and stream-based reading for large files.
โจ Features
- ๐ Efficient stream-based reading (memory-friendly for large datasets)
- ๐ Automatic type inference (
int,double,bool,DateTime,String) - ๐ Optional header support
- ๐งฉ Customizable delimiter and quote character
- โ Detailed error reporting via
CsvFormatException
๐ฆ Installation
Add the following to your pubspec.yaml:
dependencies:
ml_dataset_loader:
git:
url: https://github.com/CelkMehmett/ml_dataset_loader.git
Replace the URL with your own repository link if hosted privately or publicly.
๐ Usage
import 'package:ml_dataset_loader/ml_dataset_loader.dart';
void main() async {
final dataset = await loadCsvDataset(
'assets/iris.csv',
options: const CsvOptions(
delimiter: ',',
hasHeader: true,
quote: '"',
),
);
print('Row count: ${dataset.rowCount}');
print('First row: ${dataset.rows.first}');
}
๐ง Dataset Model
The Dataset class holds structured data as:
class Dataset {
final List<String>? headers;
final List<List<dynamic>> rows;
}
โ๏ธ CsvOptions
Customize how your CSV is parsed:
class CsvOptions {
final String delimiter; // e.g., ',', ';', '\t'
final bool hasHeader; // true if the first row is a header
final String quote; // e.g., '"'
}
๐งช Testing
Sample test with test/assets/sample.csv:
import 'package:test/test.dart';
import 'package:ml_dataset_loader/ml_dataset_loader.dart';
void main() {
test('Load sample CSV dataset', () async {
final dataset = await loadCsvDataset(
'test/assets/sample.csv',
options: const CsvOptions(hasHeader: true),
);
expect(dataset.headers, containsAll(['city', 'population']));
expect(dataset.rows.length, equals(3));
});
}
๐ File Format Example
assets/iris.csv
sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,Iris-setosa
...
๐ License
MIT ยฉ 2025 Your Name