Advertisement
chayanforyou

preferenceManager_flutter

Dec 26th, 2024
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.58 KB | None | 0 0
  1. import 'package:shared_preferences/shared_preferences.dart';
  2.  
  3. class PreferenceManager {
  4.   static const keyToken = "token";
  5.  
  6.   PreferenceManager._();
  7.  
  8.   static final PreferenceManager instance = PreferenceManager._();
  9.  
  10.   late final SharedPreferences _preference;
  11.  
  12.   Future<void> initialize() async {
  13.     _preference = await SharedPreferences.getInstance();
  14.   }
  15.  
  16.   String getString(String key, {String defaultValue = ""}) {
  17.     return _preference.getString(key) ?? defaultValue;
  18.   }
  19.  
  20.   void setString(String key, String value) {
  21.     _preference.setString(key, value);
  22.   }
  23.  
  24.   int getInt(String key, {int defaultValue = 0}) {
  25.     return _preference.getInt(key) ?? defaultValue;
  26.   }
  27.  
  28.   void setInt(String key, int value) {
  29.     _preference.setInt(key, value);
  30.   }
  31.  
  32.   double getDouble(String key, {double defaultValue = 0.0}) {
  33.     return _preference.getDouble(key) ?? defaultValue;
  34.   }
  35.  
  36.  void setDouble(String key, double value) {
  37.     _preference.setDouble(key, value);
  38.   }
  39.  
  40.   bool getBool(String key, {bool defaultValue = false}) {
  41.     return _preference.getBool(key) ?? defaultValue;
  42.   }
  43.  
  44.   void setBool(String key, bool value) {
  45.     _preference.setBool(key, value);
  46.   }
  47.  
  48.   List<String> getStringList(String key, {List<String> defaultValue = const []}) {
  49.     return _preference.getStringList(key) ?? defaultValue;
  50.   }
  51.  
  52.   void setStringList(String key, List<String> value) {
  53.     _preference.setStringList(key, value);
  54.   }
  55.  
  56.   void remove(String key) {
  57.     _preference.remove(key);
  58.   }
  59.  
  60.   void clear() {
  61.     _preference.clear();
  62.   }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement