Skip to content

Commit 2e58910

Browse files
authored
[shared_preferences] Add information about shared preferences android to docs (#8296)
Adds documentation for android shared preferences async shared preferences update.
1 parent 3976c1b commit 2e58910

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

packages/shared_preferences/shared_preferences/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.3.5
2+
3+
* Adds information about Android SharedPreferences support.
4+
15
## 2.3.4
26

37
* Security update, requires shared_preferences_android to be 2.3.4.

packages/shared_preferences/shared_preferences/README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,25 @@ latest data stored on the native platform regardless of what process was used to
5050

5151
### Android platform storage
5252

53-
The [SharedPreferences] API uses the native [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
53+
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.
54+
In most cases you should use the default option of DataStore Preferences, as it is the platform-recommended preferences storage system.
55+
However, in some cases you may need to interact with preferences that were written to SharedPreferences by code you don't control.
5456

55-
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) to store data.
57+
To use the `Android SharedPreferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync] on Android.
58+
<?code-excerpt "readme_excerpts.dart (Android_Options1)"?>
59+
```dart
60+
import 'package:shared_preferences_android/shared_preferences_android.dart';
61+
```
62+
<?code-excerpt "readme_excerpts.dart (Android_Options2)"?>
63+
```dart
64+
const SharedPreferencesAsyncAndroidOptions options =
65+
SharedPreferencesAsyncAndroidOptions(
66+
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
67+
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
68+
fileName: 'the_name_of_a_file'));
69+
```
70+
71+
The [SharedPreferences] API uses the native [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
5672

5773
## Examples
5874
Here are small examples that show you how to use the API.
@@ -187,7 +203,7 @@ the new prefix otherwise the old preferences will be inaccessible.
187203

188204
| Platform | SharedPreferences | SharedPreferencesAsync/WithCache |
189205
| :--- | :--- | :--- |
190-
| Android | SharedPreferences | DataStore Preferences |
206+
| Android | SharedPreferences | DataStore Preferences or SharedPreferences |
191207
| iOS | NSUserDefaults | NSUserDefaults |
192208
| Linux | In the XDG_DATA_HOME directory | In the XDG_DATA_HOME directory |
193209
| macOS | NSUserDefaults | NSUserDefaults |

packages/shared_preferences/shared_preferences/example/android/app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ android {
3030
main.java.srcDirs += 'src/main/kotlin'
3131
}
3232

33+
kotlinOptions {
34+
jvmTarget = '11'
35+
}
36+
3337
defaultConfig {
3438
applicationId "io.flutter.plugins.sharedpreferencesexample"
3539
minSdkVersion flutter.minSdkVersion

packages/shared_preferences/shared_preferences/example/lib/readme_excerpts.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
// ignore_for_file: public_member_api_docs, unused_local_variable, invalid_use_of_visible_for_testing_member
66
import 'package:shared_preferences/shared_preferences.dart';
7+
// #docregion Android_Options1
8+
import 'package:shared_preferences_android/shared_preferences_android.dart';
9+
// #enddocregion Android_Options1
710

811
Future<void> readmeSnippets() async {
912
// #docregion Write
@@ -92,3 +95,11 @@ Future<void> readmeTestSnippets() async {
9295
SharedPreferences.setMockInitialValues(values);
9396
// #enddocregion Tests
9497
}
98+
99+
// #docregion Android_Options2
100+
const SharedPreferencesAsyncAndroidOptions options =
101+
SharedPreferencesAsyncAndroidOptions(
102+
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
103+
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
104+
fileName: 'the_name_of_a_file'));
105+
// #enddocregion Android_Options2

packages/shared_preferences/shared_preferences/example/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ description: Demonstrates how to use the shared_preferences plugin.
33
publish_to: none
44

55
environment:
6-
sdk: ^3.4.0
7-
flutter: ">=3.22.0"
6+
sdk: ^3.5.0
7+
flutter: ">=3.24.0"
88

99
dependencies:
1010
flutter:
@@ -16,6 +16,7 @@ dependencies:
1616
# The example app is bundled with the plugin so we use a path dependency on
1717
# the parent directory to use the current plugin's version.
1818
path: ../
19+
shared_preferences_android: ^2.4.0
1920
shared_preferences_platform_interface: ^2.4.0
2021

2122
dev_dependencies:

packages/shared_preferences/shared_preferences/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
33
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
44
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
6-
version: 2.3.4
6+
version: 2.3.5
77

88
environment:
99
sdk: ^3.5.0

0 commit comments

Comments
 (0)