Update ReadData to use a generic

This commit is contained in:
Peter Resch
2020-12-12 13:37:42 -06:00
parent 259f75df32
commit f9bc1ec66c
25 changed files with 148 additions and 120 deletions
+8 -18
View File
@@ -3,29 +3,19 @@ import 'dart:io';
class ReadData {
static const prefix = 'lib/data/';
static Future<List<String>> readFileString(String classString) async {
var fileName = prefix + classString.toLowerCase() + '.data';
static Future<List<T>> readFile<T>(String classString, {bool test}) async {
var fileName = prefix + classString.toLowerCase() + (test ?? false ? 'test' : '') + '.data';
var file = File(fileName);
var contents = <String>[];
// print('open file "$fileName"');
if (await file.exists()) {
contents = await File(fileName).readAsLines();
} else {
print('File "$fileName" not found.');
}
return contents;
}
static Future<List<num>> readFileNum(String classString) async {
var fileName = prefix + classString.toLowerCase() + '.data';
var file = File(fileName);
var contents = <num>[];
var contents = <T>[];
// print('open file "$fileName"');
if (await file.exists()) {
var contentStrings = await File(fileName).readAsLines();
contents = contentStrings.map(num.parse).toList();
if (T == num) {
contents = contentStrings.map(num.parse).toList() as List<T>;
} else if (T == String) {
contents = contentStrings as List<T>;
}
} else {
print('File "$fileName" not found.');
}