Added saving/loading

This commit is contained in:
anthonycicc 2018-05-12 16:25:00 -04:00
parent 2b70494194
commit 0903e39dae
6 changed files with 79 additions and 3 deletions

View file

@ -7,12 +7,12 @@ class Entry {
Entry(this.date, this.whatYouDid);
Entry.fromJson(Map<String, dynamic> json)
: date = json['date'],
: date = DateTime.parse(json['date']),
whatYouDid = json['whatyoudid'];
Map<String, dynamic> toJson() =>
{
'date': date,
'date': date.toString(),
'whatyoudid': whatYouDid,
};

36
lib/FileLoader.dart Normal file
View file

@ -0,0 +1,36 @@
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class FileLoader {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return new File('$path/days.json');
}
Future<File> writeFile(String data) async {
final file = await _localFile;
return file.writeAsString(data);
}
Future<String> readFile() async {
try {
final file = await _localFile;
String contents = await file.readAsString();
return contents;
} catch (e) {
throw OSError("File not found");
}
}
}

View file

@ -1,7 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:nomorezerodays/Entry.dart';
import 'package:nomorezerodays/FileLoader.dart';
class NoZeroDays extends StatefulWidget {
NoZeroDays({Key key, this.title}) : super(key: key);
@ -86,7 +88,6 @@ class _NoZeroDaysState extends State<NoZeroDays> {
.add(new Entry(newValue, entry.whatYouDid));
});
}
;
Navigator.pop(context);
}).catchError((err) {
Scaffold.of(context).showSnackBar(
@ -121,6 +122,30 @@ class _NoZeroDaysState extends State<NoZeroDays> {
}));
}
void _save() {
FileLoader f = new FileLoader();
f.writeFile(json.encode(_zerodayslist));
}
void _load() {
FileLoader f = new FileLoader();
f.readFile().then((s) {
print(s);
setState(() {
List<Entry> newList = <Entry>[];
List<dynamic> tempList = json.decode(s);
for (var i = 0; i < tempList.length; ++i) {
newList.add(Entry.fromJson(tempList[i]));
}
_zerodayslist = newList;
});
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
@ -132,6 +157,10 @@ class _NoZeroDaysState extends State<NoZeroDays> {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
actions: <Widget>[
new IconButton(icon: new Icon(Icons.save), onPressed: _save),
new IconButton(icon: new Icon(Icons.file_upload), onPressed: _load),
]
),
body: new Scaffold(body: new ListView.builder(
itemBuilder: (context, entry) {

View file

@ -10,5 +10,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
</component>
</module>

View file

@ -214,6 +214,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
plugin:
dependency: transitive
description:
@ -375,3 +382,4 @@ packages:
version: "2.1.13"
sdks:
dart: ">=2.0.0-dev.52.0 <=2.0.0-edge.af1436931b93e755d38223c487d33a0a1f5eadf5"
flutter: ">=0.1.4 <2.0.0"

View file

@ -10,6 +10,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.0
path_provider: ^0.4.0
dev_dependencies:
flutter_test:
sdk: flutter