fromFile static method
Creates a Country instance by reading and parsing a CSV file.
The csvFile parameter is the full path to the country information CSV file.
Implementation
static Future<Country> fromFile(String csvFile) async {
final country = Country._();
final file = File(csvFile);
if (!await file.exists()) {
throw Exception('The CSV file $csvFile is not found.');
}
final contents = await file.readAsString();
if (contents.trim().isEmpty) {
throw Exception('Unable to read $csvFile.');
}
final rows = const CsvToListConverter().convert(contents);
if (rows.isEmpty) {
throw Exception('Invalid country information CSV file.');
}
// First row should be headers
final headers = rows.first.map((h) => h.toString()).toList();
final countryCodeIndex = headers.indexOf('country_code');
if (countryCodeIndex == -1) {
throw Exception('Invalid country information CSV file.');
}
for (var i = 1; i < rows.length; i++) {
final row = rows[i];
final rowMap = <String, String>{};
for (var j = 0; j < headers.length; j++) {
rowMap[headers[j]] = row[j]?.toString() ?? '';
}
country._records[rowMap['country_code'] ?? ''] = rowMap;
}
return country;
}