File size: 3,019 Bytes
3382f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesService {
  SharedPreferencesService._privateConstructor();

  static final SharedPreferencesService instance =
      SharedPreferencesService._privateConstructor();

  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  /// Sets a boolean [value] for the given [key] in the shared preferences.
  ///
  /// Example:
  /// ```dart
  /// await prefsService.setBool('isLoggedIn', true);
  /// ```
  Future<void> setBool(String key, bool value) async {
    final prefs = await _prefs;
    prefs.setBool(key, value);
  }

  /// Sets a string [value] for the given [key] in the shared preferences.
  ///
  /// Example:
  /// ```dart
  /// await prefsService.setString('username', 'Alice');
  /// ```
  Future<void> setString(String key, String value) async {
    final prefs = await _prefs;
    prefs.setString(key, value);
  }

  /// Sets an integer [value] for the given [key] in the shared preferences.
  ///
  /// Example:
  /// ```dart
  /// await prefsService.setInt('age', 30);
  /// ```
  Future<void> setInt(String key, int value) async {
    final prefs = await _prefs;
    prefs.setInt(key, value);
  }

  /// Sets a list of strings [value] for the given [key] in the shared preferences.
  ///
  /// Example:
  /// ```dart
  /// await prefsService.setStringList('favorites', ['Apples', 'Bananas']);
  /// ```
  Future<void> setStringList(String key, List<String> value) async {
    final prefs = await _prefs;
    prefs.setStringList(key, value);
  }

  /// Retrieves a boolean value for the given [key] from the shared preferences.
  ///
  /// Returns `null` if the key does not exist.
  ///
  /// Example:
  /// ```dart
  /// bool? isLoggedIn = await prefsService.getBool('isLoggedIn');
  /// ```
  Future<bool?> getBool(String key) async {
    final prefs = await _prefs;
    return prefs.getBool(key);
  }

  /// Retrieves a string value for the given [key] from the shared preferences.
  ///
  /// Returns `null` if the key does not exist.
  ///
  /// Example:
  /// ```dart
  /// String? username = await prefsService.getString('username');
  /// ```
  Future<String?> getString(String key) async {
    final prefs = await _prefs;
    return prefs.getString(key);
  }

  /// Retrieves an integer value for the given [key] from the shared preferences.
  ///
  /// Returns `null` if the key does not exist.
  ///
  /// Example:
  /// ```dart
  /// int? age = await prefsService.getInt('age');
  /// ```
  Future<int?> getInt(String key) async {
    final prefs = await _prefs;
    return prefs.getInt(key);
  }

  /// Retrieves a list of strings for the given [key] from the shared preferences.
  ///
  /// Returns `null` if the key does not exist.
  ///
  /// Example:
  /// ```dart
  /// List<String>? favorites = await prefsService.getStringList('favorites');
  /// ```
  Future<List<String>?> getStringList(String key) async {
    final prefs = await _prefs;
    return prefs.getStringList(key);
  }
}