Last active
January 11, 2019 03:39
-
-
Save s-rajaraman/3da88289b20d1109eadfd14cff108eb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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