Skip to content

Commit 563dd2c

Browse files
authored
[shared_preferences] Adds Shared preferences as option in shared preferences async android (#7994)
Adds the ability to select which Android preferences backend (SharedPreferences or DataStore Preferences) one would like to use. Also adds the ability to pick a file name for the shared preferences backend. fixes flutter/flutter#153300 fixes flutter/flutter#14337
1 parent 56886ff commit 563dd2c

File tree

27 files changed

+1478
-838
lines changed

27 files changed

+1478
-838
lines changed

packages/shared_preferences/shared_preferences_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.0
2+
3+
* Adds `SharedPreferences` support within `SharedPreferencesAsyncAndroid` API.
4+
15
## 2.3.4
26

37
* Restrict types when decoding preferences.

packages/shared_preferences/shared_preferences_android/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,22 @@ so you do not need to add it to your `pubspec.yaml`.
1111
However, if you `import` this package to use any of its APIs directly, you
1212
should add it to your `pubspec.yaml` as usual.
1313

14+
## Options
15+
16+
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs can use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) or [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) to store data.
17+
18+
To use the `Android SharedPreferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync].
19+
20+
<?code-excerpt "example/lib/main.dart (Android_Options)"?>
21+
```dart
22+
const SharedPreferencesAsyncAndroidOptions options =
23+
SharedPreferencesAsyncAndroidOptions(
24+
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
25+
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
26+
fileName: 'the_name_of_a_file'));
27+
```
28+
29+
The [SharedPreferences] API uses the native [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
30+
1431
[1]: https://pub.dev/packages/shared_preferences
1532
[2]: https://flutter.dev/to/endorsed-federated-plugin

packages/shared_preferences/shared_preferences_android/android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ android {
6060
}
6161
dependencies {
6262
implementation 'androidx.datastore:datastore:1.0.0'
63-
implementation 'androidx.datastore:datastore-preferences:1.0.0'
63+
implementation 'androidx.datastore:datastore-preferences:1.0.0'
64+
implementation 'androidx.preference:preference:1.2.1'
6465
testImplementation 'junit:junit:4.13.2'
6566
testImplementation 'androidx.test:core-ktx:1.5.0'
6667
testImplementation 'androidx.test.ext:junit-ktx:1.2.1'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.gradle.jvmargs=-Xmx4G
2+
android.useAndroidX=true
3+
android.enableJetifier=true

0 commit comments

Comments
 (0)