import 'dart:convert'; import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:todo/buisness/ToDoItem.dart'; import 'package:todo/database/DatabaseInterface.dart'; class PathProvider implements DatabaseInterface { Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } Future get _localFile async { final path = await _localPath; print(path); File file = File('$path/dataSeq.txt'); if (!file.existsSync()) { file = await file.create(); await file.writeAsString('[]'); } return file; } @override Future> getToDoItems() async { final file = await _localFile; final contents = await file.readAsString(); final contentJson = jsonDecode(contents); List contentList = contentJson is List ? contentJson : [contentJson]; List toDoList = contentList.map((item) => ToDoItem.fromJson(item)).toList(); return toDoList; } @override Future insertToDoItem(ToDoItem item) async { item.id = DateTime.now().millisecondsSinceEpoch; List l = await getToDoItems(); l.add(item); final file = await _localFile; return file.writeAsString(jsonEncode(l.map((item) => item.toJson()).toList())); } @override Future updateToDoItem(ToDoItem item) async { final file = await _localFile; return file.writeAsString(jsonEncode(item.toJson())); } @override Future deleteToDoItem(int id) async { final file = await _localFile; List l = await getToDoItems(); l.remove(l.firstWhere((item) => item.id == id)); return file.writeAsString(jsonEncode(l.map((item) => item.toJson()).toList())); } }