Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## NEXT

* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
* Updates minimum supported SDK version to Flutter 3.29/Dart 3.7.
* Updates README to indicate that Andoid SDK <21 is no longer supported.

## 2.5.3
Expand Down
12 changes: 7 additions & 5 deletions packages/shared_preferences/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ import 'package:shared_preferences_android/shared_preferences_android.dart';
```dart
const SharedPreferencesAsyncAndroidOptions options =
SharedPreferencesAsyncAndroidOptions(
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
fileName: 'the_name_of_a_file'));
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.
Expand Down Expand Up @@ -136,8 +138,8 @@ await asyncPrefs.clear(allowList: <String>{'action', 'repeat'});
### SharedPreferencesWithCache
<?code-excerpt "readme_excerpts.dart (WithCache)"?>
```dart
final SharedPreferencesWithCache prefsWithCache =
await SharedPreferencesWithCache.create(
final SharedPreferencesWithCache
prefsWithCache = await SharedPreferencesWithCache.create(
cacheOptions: const SharedPreferencesWithCacheOptions(
// When an allowlist is included, any keys that aren't included cannot be used.
allowList: <String>{'repeat', 'action'},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,29 @@ void main() {
});

group('SharedPreferences with setPrefix and allowList', () {
runAllGroups(
() {
final Set<String> allowList = <String>{
'prefix.$boolKey',
'prefix.$intKey',
'prefix.$doubleKey',
'prefix.$listKey'
};
SharedPreferences.setPrefix('prefix.', allowList: allowList);
},
stringValue: null,
);
runAllGroups(() {
final Set<String> allowList = <String>{
'prefix.$boolKey',
'prefix.$intKey',
'prefix.$doubleKey',
'prefix.$listKey',
};
SharedPreferences.setPrefix('prefix.', allowList: allowList);
}, stringValue: null);
});

group('SharedPreferences with prefix set to empty string', () {
runAllGroups(
() {
SharedPreferences.setPrefix('');
},
keysCollide: true,
);
runAllGroups(() {
SharedPreferences.setPrefix('');
}, keysCollide: true);
});
}

void runAllGroups(void Function() legacySharedPrefsConfig,
{String? stringValue = testString, bool keysCollide = false}) {
void runAllGroups(
void Function() legacySharedPrefsConfig, {
String? stringValue = testString,
bool keysCollide = false,
}) {
group('default sharedPreferencesAsyncOptions', () {
const SharedPreferencesOptions sharedPreferencesAsyncOptions =
SharedPreferencesOptions();
Expand All @@ -84,21 +81,22 @@ void runAllGroups(void Function() legacySharedPrefsConfig,
if (Platform.isAndroid) {
sharedPreferencesAsyncOptions =
const SharedPreferencesAsyncAndroidOptions(
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
fileName: 'fileName',
),
);
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions:
AndroidSharedPreferencesStoreOptions(fileName: 'fileName'),
);
} else if (Platform.isIOS || Platform.isMacOS) {
sharedPreferencesAsyncOptions =
SharedPreferencesAsyncFoundationOptions(suiteName: 'group.fileName');
sharedPreferencesAsyncOptions = SharedPreferencesAsyncFoundationOptions(
suiteName: 'group.fileName',
);
} else if (Platform.isLinux) {
sharedPreferencesAsyncOptions = const SharedPreferencesLinuxOptions(
fileName: 'fileName',
);
} else if (Platform.isWindows) {
sharedPreferencesAsyncOptions =
const SharedPreferencesWindowsOptions(fileName: 'fileName');
sharedPreferencesAsyncOptions = const SharedPreferencesWindowsOptions(
fileName: 'fileName',
);
} else {
sharedPreferencesAsyncOptions = const SharedPreferencesOptions();
}
Expand All @@ -114,10 +112,10 @@ void runAllGroups(void Function() legacySharedPrefsConfig,
group('Android default sharedPreferences', () {
const SharedPreferencesOptions sharedPreferencesAsyncOptions =
SharedPreferencesAsyncAndroidOptions(
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions:
AndroidSharedPreferencesStoreOptions(),
);
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
originalSharedPreferencesOptions:
AndroidSharedPreferencesStoreOptions(),
);

runTests(
sharedPreferencesAsyncOptions,
Expand All @@ -128,9 +126,12 @@ void runAllGroups(void Function() legacySharedPrefsConfig,
}
}

void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
void Function() legacySharedPrefsConfig,
{String? stringValue = testString, bool keysAndNamesCollide = false}) {
void runTests(
SharedPreferencesOptions sharedPreferencesAsyncOptions,
void Function() legacySharedPrefsConfig, {
String? stringValue = testString,
bool keysAndNamesCollide = false,
}) {
setUp(() async {
// Configure and populate the source legacy shared preferences.
SharedPreferences.resetStatic();
Expand All @@ -146,8 +147,9 @@ void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
});

tearDown(() async {
await SharedPreferencesAsync(options: sharedPreferencesAsyncOptions)
.clear();
await SharedPreferencesAsync(
options: sharedPreferencesAsyncOptions,
).clear();
});

testWidgets('data is successfully transferred to new system', (_) async {
Expand All @@ -158,8 +160,9 @@ void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
migrationCompletedKey: migrationCompletedKey,
);

final SharedPreferencesAsync asyncPreferences =
SharedPreferencesAsync(options: sharedPreferencesAsyncOptions);
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync(
options: sharedPreferencesAsyncOptions,
);

expect(await asyncPreferences.getBool(boolKey), testBool);
expect(await asyncPreferences.getInt(intKey), testInt);
Expand All @@ -176,8 +179,9 @@ void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
migrationCompletedKey: migrationCompletedKey,
);

final SharedPreferencesAsync asyncPreferences =
SharedPreferencesAsync(options: sharedPreferencesAsyncOptions);
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync(
options: sharedPreferencesAsyncOptions,
);

expect(await asyncPreferences.getBool(migrationCompletedKey), true);
});
Expand All @@ -193,8 +197,9 @@ void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
migrationCompletedKey: migrationCompletedKey,
);

final SharedPreferencesAsync asyncPreferences =
SharedPreferencesAsync(options: sharedPreferencesAsyncOptions);
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync(
options: sharedPreferencesAsyncOptions,
);
await preferences.setInt(intKey, -0);
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
legacySharedPreferencesInstance: preferences,
Expand All @@ -204,7 +209,8 @@ void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions,
expect(await asyncPreferences.getInt(intKey), testInt);
},
// Skips platforms that would be adding the preferences to the same file.
skip: keysAndNamesCollide &&
skip:
keysAndNamesCollide &&
(Platform.isWindows ||
Platform.isLinux ||
Platform.isMacOS ||
Expand Down
Loading