Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Last active November 13, 2018 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/bc4a57e305785a50ca50adc79adc9ab0 to your computer and use it in GitHub Desktop.
Save rjlutz/bc4a57e305785a50ca50adc79adc9ab0 to your computer and use it in GitHub Desktop.
helpful artifacts for the flutter student picker app
// thanks - https://flutter.io/docs/cookbook/persistence/reading-writing-files
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'student.dart';
class Storage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/names.txt');
}
Future<List<Student>> readStudents() async {
List<Student> names;
try {
final file = await _localFile;
names = List<Student>();
await file.readAsLines().then((List<String> lines) {
for (int i = 0; i < lines.length; i++) {
List<String> tokens = lines[i].split(',');
bool vis = tokens[1] == 'true' ? true : false;
names.add(new Student(tokens[0], vis));
}
});
// Read the file
} catch (e) {
print('Whoa Nellie: ' + e.toString());
return null;
}
return names;
}
Future<File> writeStudents(List<Student> names) async {
final file = await _localFile; // Write the file
var sink = file.openWrite(mode: FileMode.write);
for (int i = 0; i < names.length; i++) {
var line = names[i].name + ',' + (names[i].hidden ? 'true' : 'false') +
'\n';
sink.write(line);
}
await sink.flush();
await sink.close();
return file;
}
}
class Student {
final String name;
bool hidden;
Student(this.name, this.hidden);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment