2021-10-26 18:50:34 +02:00
|
|
|
import 'dart:core';
|
|
|
|
|
2022-02-17 08:50:11 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-02-03 14:50:03 +01:00
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
class Store {
|
2022-02-17 08:50:11 +01:00
|
|
|
SharedPreferences? _prefs;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-10-25 16:59:55 +01:00
|
|
|
Future<void> _setupLocalStorage() async {
|
2022-02-17 08:50:11 +01:00
|
|
|
_prefs ??= await SharedPreferences.getInstance();
|
2020-10-25 16:59:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Future<String?> getItem(String key) async {
|
2022-02-17 08:50:11 +01:00
|
|
|
await _setupLocalStorage();
|
|
|
|
return _prefs!.getString(key);
|
2020-01-26 12:17:54 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Future<bool> getItemBool(String key, [bool? defaultValue]) async {
|
2022-02-17 08:50:11 +01:00
|
|
|
await _setupLocalStorage();
|
|
|
|
return _prefs!.getBool(key) ?? defaultValue ?? true;
|
2020-11-07 12:00:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
Future<void> setItem(String key, String? value) async {
|
2022-02-17 08:50:11 +01:00
|
|
|
await _setupLocalStorage();
|
|
|
|
if (value == null) {
|
|
|
|
await _prefs!.remove(key);
|
|
|
|
return;
|
2021-02-03 16:30:23 +01:00
|
|
|
}
|
2022-02-17 08:50:11 +01:00
|
|
|
await _prefs!.setString(key, value);
|
|
|
|
return;
|
2020-01-26 12:17:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 14:06:00 +01:00
|
|
|
Future<void> setItemBool(String key, bool value) async {
|
2022-02-17 08:50:11 +01:00
|
|
|
await _setupLocalStorage();
|
|
|
|
await _prefs!.setBool(key, value);
|
|
|
|
return;
|
2021-01-23 14:06:00 +01:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:59:55 +01:00
|
|
|
Future<void> deleteItem(String key) async {
|
2022-02-17 08:50:11 +01:00
|
|
|
await _setupLocalStorage();
|
|
|
|
await _prefs!.remove(key);
|
|
|
|
return;
|
2020-03-29 12:06:25 +02:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|