Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.4

* Adds information about Android SharedPreferences support.

## 2.3.3

* Clarifies scope of prefix handling in README.
Expand Down
19 changes: 16 additions & 3 deletions packages/shared_preferences/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,22 @@ latest data stored on the native platform regardless of what process was used to

### Android platform storage

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

The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) to store data.
To use the `Android SharedPreferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync] on Android.

<?code-excerpt "readme_excerpts.dart (Android_Options)"?>
```dart
const SharedPreferencesAsyncAndroidOptions options =
SharedPreferencesAsyncAndroidOptions(
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
fileName: 'the_name_of_a_file'));
```

The [SharedPreferences] API uses the native [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.

## Examples
Here are small examples that show you how to use the API.
Expand Down Expand Up @@ -187,7 +200,7 @@ the new prefix otherwise the old preferences will be inaccessible.

| Platform | SharedPreferences | SharedPreferencesAsync/WithCache |
| :--- | :--- | :--- |
| Android | SharedPreferences | DataStore Preferences |
| Android | SharedPreferences | DataStore Preferences or SharedPreferences |
| iOS | NSUserDefaults | NSUserDefaults |
| Linux | In the XDG_DATA_HOME directory | In the XDG_DATA_HOME directory |
| macOS | NSUserDefaults | NSUserDefaults |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ android {
main.java.srcDirs += 'src/main/kotlin'
}

kotlinOptions {
jvmTarget = '11'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
applicationId "io.flutter.plugins.sharedpreferencesexample"
minSdkVersion flutter.minSdkVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// ignore_for_file: public_member_api_docs, unused_local_variable, invalid_use_of_visible_for_testing_member
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_android/shared_preferences_android.dart';

Future<void> readmeSnippets() async {
// #docregion Write
Expand Down Expand Up @@ -92,3 +93,11 @@ Future<void> readmeTestSnippets() async {
SharedPreferences.setMockInitialValues(values);
// #enddocregion Tests
}

// #docregion Android_Options
const SharedPreferencesAsyncAndroidOptions options =
SharedPreferencesAsyncAndroidOptions(
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
fileName: 'the_name_of_a_file'));
// #enddocregion Android_Options
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: Demonstrates how to use the shared_preferences plugin.
publish_to: none

environment:
sdk: ^3.4.0
flutter: ">=3.22.0"
sdk: ^3.5.0
flutter: ">=3.24.0"

dependencies:
flutter:
Expand All @@ -16,6 +16,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
shared_preferences_android: ^2.4.0
shared_preferences_platform_interface: ^2.4.0

dev_dependencies:
Expand Down
6 changes: 3 additions & 3 deletions packages/shared_preferences/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.3.3
version: 2.3.4

environment:
sdk: ^3.4.0
flutter: ">=3.22.0"
sdk: ^3.5.0
flutter: ">=3.24.0"

flutter:
plugin:
Expand Down
Loading