import 'dart:io' show File, FileMode; import 'package:intl/intl.dart' show DateFormat; import 'package:gps/models/gps_point.dart'; abstract class GpsPersister { void save(Iterable data); } class CsvGpsPersister implements GpsPersister { static const String _csvHeader = "#Time, Latitude, Longitude, Accuracy, Speed, Heading"; /// Format of DateTime part of file name static final DateFormat _formatter = DateFormat('yyyyMMdd_HHmmss'); /// Path where the CSV will be stored final String _rootDir; const CsvGpsPersister(this._rootDir); @override void save(Iterable points) { File file = File("$_rootDir/geo${_formatter.format(DateTime.now())}.csv"); file.writeAsStringSync(_csvHeader); for (var point in points) { file.writeAsStringSync("\n${_toCsv(point)}", mode: FileMode.writeOnlyAppend); } file.writeAsString("\n", mode: FileMode.writeOnlyAppend, flush: true); } String _toCsv(GpsPoint p) => "${p.time}, ${p.latitude}, ${p.longitude}, ${p.accuracy}, ${p.speed}, ${p.heading}"; }