-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: shared preferences + local storage
- Loading branch information
Showing
7 changed files
with
78 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:localstorage/localstorage.dart'; | ||
|
||
class LocalStorageService { | ||
LocalStorageService._privateConstructor(); | ||
final String storageName; | ||
late final LocalStorage localStorage; | ||
|
||
static final LocalStorageService instance = LocalStorageService._privateConstructor(); | ||
LocalStorageService._privateConstructor({required this.storageName}) { | ||
localStorage = LocalStorage(storageName); | ||
} | ||
|
||
static final LocalStorageService global = LocalStorageService.getStorage("global"); | ||
|
||
static LocalStorageService getStorage(String storageName) => | ||
LocalStorageService._privateConstructor(storageName: storageName); | ||
|
||
Future<String?> getString(String key, {String? defaultValue}) async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
return prefs.getString(key) ?? defaultValue; | ||
Future<dynamic> get(String key, {String? defaultValue}) async { | ||
await localStorage.ready; | ||
return localStorage.getItem(key); | ||
} | ||
|
||
Future<void> putString(String key, String value) async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
await prefs.setString(key, value); | ||
Future<void> put(String key, dynamic value) async { | ||
await localStorage.setItem(key, value); | ||
} | ||
|
||
Future<void> clear() async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
await prefs.clear(); | ||
await localStorage.clear(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class SharedPreferencesService { | ||
SharedPreferencesService._privateConstructor(); | ||
|
||
static final SharedPreferencesService instance = SharedPreferencesService._privateConstructor(); | ||
|
||
Future<String?> getString(String key, {String? defaultValue}) async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
return prefs.getString(key) ?? defaultValue; | ||
} | ||
|
||
Future<void> putString(String key, String value) async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
await prefs.setString(key, value); | ||
} | ||
|
||
Future<void> clear() async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
await prefs.clear(); | ||
} | ||
} |
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
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
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