alwas use new instance on shared preferences

main
Julian Gegner 2023-02-19 15:06:57 +01:00
parent 66fed9d072
commit f67f44a030
1 changed files with 8 additions and 7 deletions

View File

@ -2,27 +2,28 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/models/settings.dart';
import 'package:smoke_cess_app/service/json_service.dart'; import 'package:smoke_cess_app/service/json_service.dart';
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
//access group setting which was saved in local storage //access group setting which was saved in local storage
Future<String?> getGroup() { Future<String?> getGroup() {
return getStringSetting('group'); return getStringSetting('group');
} }
void setStringSetting(String settingKey, String settingValue) => void setStringSetting(String settingKey, String settingValue) =>
_prefs.then((pref) => pref.setString(settingKey, settingValue)); SharedPreferences.getInstance()
.then((pref) => pref.setString(settingKey, settingValue));
Future<String?> getStringSetting(String settingKey) => Future<String?> getStringSetting(String settingKey) =>
_prefs.then((pref) => pref.getString(settingKey)); SharedPreferences.getInstance().then((pref) => pref.getString(settingKey));
void setIntSetting(String settingKey, int settingValue) => void setIntSetting(String settingKey, int settingValue) =>
_prefs.then((pref) => pref.setInt(settingKey, settingValue)); SharedPreferences.getInstance()
.then((pref) => pref.setInt(settingKey, settingValue));
Future<int?> getIntSetting(String settingKey) => Future<int?> getIntSetting(String settingKey) =>
_prefs.then((pref) => pref.getInt(settingKey)); SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey));
void setStringList(String settingKey, List<String> list) => void setStringList(String settingKey, List<String> list) =>
_prefs.then((pref) => pref.setStringList(settingKey, list)); SharedPreferences.getInstance()
.then((pref) => pref.setStringList(settingKey, list));
//Add other setters and getters if needed //Add other setters and getters if needed
//other possible SharedPreferences Types: Int, Bool, Double, StringList //other possible SharedPreferences Types: Int, Bool, Double, StringList