From 8869881a5b897768a325011b6708b6cf9f93b527 Mon Sep 17 00:00:00 2001 From: OlehMarch Date: Wed, 27 Sep 2023 23:38:26 +0300 Subject: [PATCH] added setPreferencesDirectory for json local storage --- lib/src/core/local_storage.json.dart | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/src/core/local_storage.json.dart b/lib/src/core/local_storage.json.dart index c7bacff..89b5cf9 100644 --- a/lib/src/core/local_storage.json.dart +++ b/lib/src/core/local_storage.json.dart @@ -25,7 +25,13 @@ class LocalStorageStore implements LocalStorageInterface { /// in sync since the setter method might fail for any reason. final Map? _preferenceCache; + static String _prefFileDir = ''; + static void setPreferencesDirectory(String directory) => + _prefFileDir = directory; + static const String _prefFileName = 'preferences.json'; + static String get _prefFilePath => + _prefFileDir.isNotEmpty ? '$_prefFileDir/$_prefFileName' : _prefFileName; static LocalStorageStore? _instance; static FutureOr getInstance() async { @@ -162,14 +168,14 @@ class LocalStorageStore implements LocalStorageInterface { } Future _commit() async { - final file = File(_prefFileName); + final file = File(_prefFilePath); final jsonString = json.encode(_preferenceCache); await file.writeAsString(jsonString); return true; } static Future?> _getSharedPreferencesMap() async { - final file = File(_prefFileName); + final file = File(_prefFilePath); if (await file.exists()) { final jsonString = await file.readAsString(); if (jsonString.isNotEmpty == true) {