fromCsv static method

Future<ImportResult> fromCsv(
  1. String filePath, {
  2. String? schemaName,
  3. String delimiter = ',',
  4. bool hasHeader = true,
})

Imports from a CSV file

Implementation

static Future<ImportResult> fromCsv(
  String filePath, {
  String? schemaName,
  String delimiter = ',',
  bool hasHeader = true,
}) async {
  try {
    final file = File(filePath);
    if (!await file.exists()) {
      return ImportResult(
        schema: _createEmptySchema(schemaName ?? 'ImportedData'),
        data: [],
        rowCount: 0,
        error: 'File not found: $filePath',
      );
    }

    final content = await file.readAsString();
    return fromCsvString(
      content,
      schemaName: schemaName ?? _extractFileName(filePath),
      delimiter: delimiter,
      hasHeader: hasHeader,
    );
  } catch (e) {
    return ImportResult(
      schema: _createEmptySchema(schemaName ?? 'ImportedData'),
      data: [],
      rowCount: 0,
      error: 'Error reading the file: $e',
    );
  }
}