import 'package:path_provider/path_provider.dart'; import 'dart:convert'; import 'dart:io'; class FileHandler { static Future get localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } static Future get localFile async { final path = await localPath; return File("$path/todos.json"); } static Future get fileContent async { await existCheck(); File f = await localFile; String content = await f.readAsString(); final js = await json.decode(content); return js; } static Future saveNotified(String listName, String todoName, String notifyType, bool value) async { Map content = await fileContent; content[listName][todoName][notifyType] = value; await saveFile(content); } static Future saveFile(js) async { File f = await localFile; String content = json.encode(js); f.writeAsString(content); } static Future saveList(js, String listName) async { final content = await fileContent; content[listName] = js; await saveFile(content); } static Future renameList(String oldListName, String newListName) async { var newJs = {}; final content = await fileContent; for (String key in content.keys) { if (key != oldListName) { newJs[key] = content[key]; } else { newJs[newListName] = content[key]; } } await saveFile(newJs); } static Future addList(String listName) async { var js = await fileContent; js[listName] = {}; await saveFile(js); } static Future removeList(String listName) async { final js = await fileContent; var newJs = {}; for (String key in js.keys) { if (key != listName) { newJs[key] = js[key]; } } await saveFile(newJs); } static Future existCheck() async { File f = await localFile; bool exists = await f.exists(); if (!exists) { await saveFile({"Personal": {}}); } } }