Skip to content

Instantly share code, notes, and snippets.

@s-rajaraman
Last active January 11, 2019 03:39
Show Gist options
  • Save s-rajaraman/3da88289b20d1109eadfd14cff108eb6 to your computer and use it in GitHub Desktop.
Save s-rajaraman/3da88289b20d1109eadfd14cff108eb6 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class TodoStorage {
static Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get _localFile async {
final path = await _localPath;
final file = File('$path/todo.txt');
file.createSync();
return file;
}
static Future<List<String>> readAllTodos() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
return contents.split("\n");
} catch (e) {
print(e);
return new List<String>.generate(10, (i) => "Item $i");
}
}
static Future<List<String>> addNewTodo(String todo) async {
final file = await _localFile;
String contents = await file.readAsString() + "\n" + todo;
// Write the file
file.writeAsString(contents);
return contents.split("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment