-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.dart
45 lines (38 loc) · 1.23 KB
/
import.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import 'dart:convert';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'model/ascent.dart';
class CsvImporter {
Future<List<Ascent>?> readFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path!);
final contents = await file.readAsString();
return parse(contents);
} else {
return null;
}
}
List<Ascent> parse(String contents) {
LineSplitter splitter = new LineSplitter();
var lines = splitter.convert(contents);
List<Ascent> ascents = lines.map((line) => Ascent.fromString(line)).toList();
return ascents;
}
Future<void> writeFile(List<Ascent> ascents) async {
String? directory = await FilePicker.platform.getDirectoryPath();
if (directory != null) {
File file = File(directory + "/ascent-export.csv");
print("exporting to $file");
StringBuffer buf = StringBuffer();
for (Ascent a in ascents) {
buf.write(a.encode());
}
file.writeAsString(buf.toString());
}
}
List<Ascent> parseEightAJson(String data) {
List list = json.decode(data);
return list.map((item) => Ascent.fromJson(item)).toList();
}
}