You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -17,9 +17,48 @@ Supported data types are `int`, `double`, `bool`, `String` and `List<String>`.
17
17
18
18
## Usage
19
19
20
-
### Examples
20
+
## SharedPreferences vs SharedPreferencesAsync vs SharedPreferencesWithCache
21
+
22
+
Starting with version 2.3.0 there are three available APIs that can be used in this package.
23
+
[SharedPreferences] is a legacy API that will be deprecated in the future. We highly encourage
24
+
any new users of the plugin to use the newer [SharedPreferencesAsync] or [SharedPreferencesWithCache]
25
+
APIs instead.
26
+
27
+
Consider migrating existing code to one of the new APIs. See [below](#migrating-from-sharedpreferences-to-sharedpreferencesasyncwithcache)
28
+
for more information.
29
+
30
+
### Cache and async or sync getters
31
+
32
+
[SharedPreferences] and [SharedPreferencesWithCache] both use a local cache to store preferences.
33
+
This allows for synchronous get calls after the initial setup call fetches the preferences from the platform.
34
+
However, The cache can present issues as well:
35
+
36
+
- If you are using `shared_preferences` from multiple isolates, since each
37
+
isolate has its own singleton and cache.
38
+
- If you are using `shared_preferences` in multiple engine instances (including
39
+
those created by plugins that create background contexts on mobile devices,
40
+
such as `firebase_messaging`).
41
+
- If you are modifying the underlying system preference store through something
42
+
other than the `shared_preferences` plugin, such as native code.
43
+
44
+
This can be remedied by calling the `reload` method before using a getter as needed.
45
+
If most get calls need a reload, consider using [SharedPreferencesAsync] instead.
46
+
47
+
[SharedPreferencesAsync] does not utilize a local cache which causes all calls to be asynchronous
48
+
calls to the host platforms storage solution. This can be less performant, but should always provide the
49
+
latest data stored on the native platform regardless of what process was used to store it.
50
+
51
+
### Android platform storage
52
+
53
+
The [SharedPreferences] API uses the native [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
54
+
55
+
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) to store data.
56
+
57
+
## Examples
21
58
Here are small examples that show you how to use the API.
22
59
60
+
### SharedPreferences
61
+
23
62
#### Write data
24
63
<?code-excerpt "readme_excerpts.dart (Write)"?>
25
64
```dart
@@ -60,32 +99,67 @@ final List<String>? items = prefs.getStringList('items');
60
99
await prefs.remove('counter');
61
100
```
62
101
63
-
### Multiple instances
102
+
### SharedPreferencesAsync
103
+
<?code-excerpt "readme_excerpts.dart (Async)"?>
104
+
```dart
105
+
final SharedPreferencesAsync asyncPrefs = SharedPreferencesAsync();
64
106
65
-
In order to make preference lookup via the `get*` methods synchronous,
66
-
`shared_preferences` uses a cache on the Dart side, which is normally only
67
-
updated by the `set*` methods. Usually this is an implementation detail that
68
-
does not affect callers, but it can cause issues in a few cases:
69
-
- If you are using `shared_preferences` from multiple isolates, since each
70
-
isolate has its own `SharedPreferences` singleton and cache.
71
-
- If you are using `shared_preferences` in multiple engine instances (including
72
-
those created by plugins that create background contexts on mobile devices,
73
-
such as `firebase_messaging`).
74
-
- If you are modifying the underlying system preference store through something
75
-
other than the `shared_preferences` plugin, such as native code.
107
+
await asyncPrefs.setBool('repeat', true);
108
+
await asyncPrefs.setString('action', 'Start');
109
+
110
+
final bool? repeat = await asyncPrefs.getBool('repeat');
111
+
final String? action = await asyncPrefs.getString('action');
112
+
113
+
await asyncPrefs.remove('repeat');
114
+
115
+
// Any time a filter option is included as a method parameter, strongly consider
116
+
// using it to avoid potentially unwanted side effects.
0 commit comments