Skip to content

[shared_preferences] fix cast error and mutable list error with getStringList #7355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 9, 2024
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,3 +1,7 @@
## 2.3.1

* Fixes `getStringList` returning immutable list.

## 2.3.0

* Adds new `SharedPreferencesAsyncAndroid` API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,16 @@ void main() {
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList returns mutable list', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
final List<String>? list =
await preferences.getStringList(listKey, emptyOptions);
list?.add('value');
expect(list?.length, testList.length + 1);
});

testWidgets('getPreferences', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();
await Future.wait(<Future<void>>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ base class SharedPreferencesAsyncAndroid
// is fixed. In practice, the values will never be null, and the native implementation assumes that.
return _convertKnownExceptions<List<String>>(() async =>
(await _api.getStringList(key, _convertOptionsToPigeonOptions(options)))
?.cast<String>());
?.cast<String>()
.toList());
}

Future<T?> _convertKnownExceptions<T>(Future<T?> Function() method) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_android
description: Android implementation of the shared_preferences plugin
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.3.0
version: 2.3.1

environment:
sdk: ^3.4.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.5.1

* Fixes `getStringList` returning immutable list.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

## 2.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,16 @@ void main() {
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList returns mutable list', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
final List<String>? list =
await preferences.getStringList(listKey, emptyOptions);
list?.add('value');
expect(list?.length, testList.length + 1);
});

testWidgets('getPreferences', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();
await Future.wait(<Future<void>>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ base class SharedPreferencesAsyncFoundation
return _convertKnownExceptions<List<String>>(() async =>
((await _api.getValue(key, _convertOptionsToPigeonOptions(options)))
as List<Object?>?)
?.cast<String>());
?.cast<String>()
.toList());
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_foundation
description: iOS and macOS implementation of the shared_preferences plugin.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_foundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.5.0
version: 2.5.1

environment:
sdk: ^3.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.4.1

* Fixes `getStringList` returning immutable list.
* Fixes `getStringList` cast error.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

## 2.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,15 @@ void main() {
const double testDouble = 3.14159;
const List<String> testList = <String>['foo', 'bar'];

Future<SharedPreferencesAsyncPlatform> getPreferences() async {
Future<SharedPreferencesAsyncPlatform> getPreferences(
{bool clear = true}) async {
final SharedPreferencesAsyncPlatform preferences =
SharedPreferencesAsyncPlatform.instance!;
await preferences.clear(
const ClearPreferencesParameters(filter: PreferencesFilters()),
emptyOptions);
if (clear) {
await preferences.clear(
const ClearPreferencesParameters(filter: PreferencesFilters()),
emptyOptions);
}
return preferences;
}

Expand Down Expand Up @@ -397,6 +400,24 @@ void main() {
await preferences.setStringList(listKey, testList, emptyOptions);
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});
testWidgets('getStringList does not throw cast error',
(WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
await (preferences as SharedPreferencesAsyncLinux).reload(emptyOptions);
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList returns mutable list', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
final List<String>? list =
await preferences.getStringList(listKey, emptyOptions);
list?.add('value');
expect(list?.length, testList.length + 1);
});

testWidgets('getPreferences', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ base class SharedPreferencesAsyncLinux extends SharedPreferencesAsyncPlatform {
SharedPreferencesOptions options,
) async {
final Map<String, Object> data = await _readAll(<String>{key}, options);
return (data[key] as List<String>?)?.toList();
return (data[key] as List<Object?>?)?.cast<String>().toList();
}

@override
Expand Down Expand Up @@ -282,6 +282,14 @@ base class SharedPreferencesAsyncLinux extends SharedPreferencesAsyncPlatform {
return _readAll(parameters.filter.allowList, options);
}

/// Reloads preferences from file.
@visibleForTesting
Future<void> reload(
SharedPreferencesLinuxOptions options,
) async {
_cachedPreferences = await _reload(options.fileName);
}

Future<Map<String, Object>> _readAll(
Set<String>? allowList,
SharedPreferencesOptions options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_linux
description: Linux implementation of the shared_preferences plugin
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_linux
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.4.0
version: 2.4.1

environment:
sdk: ^3.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.4.2

* Fixes `getStringList` returning immutable list.
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.

## 2.4.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ void main() {
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList returns mutable list', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
final List<String>? list =
await preferences.getStringList(listKey, emptyOptions);
list?.add('value');
expect(list?.length, testList.length + 1);
});

testWidgets('getPreferences', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();
await Future.wait(<Future<void>>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ base class SharedPreferencesAsyncWeb extends SharedPreferencesAsyncPlatform {
) async {
final Map<String, Object> data =
await _readAllFromLocalStorage(<String>{key}, options);
return data[key] as List<String>?;
return (data[key] as List<String>?)?.toList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_web
description: Web platform implementation of shared_preferences
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.4.1
version: 2.4.2

environment:
sdk: ^3.4.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.4.1

* Fixes `getStringList` returning immutable list.
* Fixes `getStringList` cast error.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

## 2.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,15 @@ void main() {
const double testDouble = 3.14159;
const List<String> testList = <String>['foo', 'bar'];

Future<SharedPreferencesAsyncPlatform> getPreferences() async {
Future<SharedPreferencesAsyncPlatform> getPreferences(
{bool clear = true}) async {
final SharedPreferencesAsyncPlatform preferences =
SharedPreferencesAsyncPlatform.instance!;
await preferences.clear(
const ClearPreferencesParameters(filter: PreferencesFilters()),
emptyOptions);
if (clear) {
await preferences.clear(
const ClearPreferencesParameters(filter: PreferencesFilters()),
emptyOptions);
}
return preferences;
}

Expand Down Expand Up @@ -398,6 +401,25 @@ void main() {
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList does not throw cast error',
(WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
await (preferences as SharedPreferencesAsyncWindows).reload(emptyOptions);
expect(await preferences.getStringList(listKey, emptyOptions), testList);
});

testWidgets('getStringList returns mutable list', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();

await preferences.setStringList(listKey, testList, emptyOptions);
final List<String>? list =
await preferences.getStringList(listKey, emptyOptions);
list?.add('value');
expect(list?.length, testList.length + 1);
});

testWidgets('getPreferences', (WidgetTester _) async {
final SharedPreferencesAsyncPlatform preferences = await getPreferences();
await preferences.setString(stringKey, testString, emptyOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ base class SharedPreferencesAsyncWindows
SharedPreferencesOptions options,
) async {
final Map<String, Object> data = await _readAll(<String>{key}, options);
return (data[key] as List<String>?)?.toList();
return (data[key] as List<Object?>?)?.cast<String>().toList();
}

@override
Expand Down Expand Up @@ -283,6 +283,14 @@ base class SharedPreferencesAsyncWindows
return _readAll(parameters.filter.allowList, options);
}

/// Reloads preferences from file.
@visibleForTesting
Future<void> reload(
SharedPreferencesWindowsOptions options,
) async {
_cachedPreferences = await _readFromFile(options.fileName);
}

Future<Map<String, Object>> _readAll(
Set<String>? allowList,
SharedPreferencesOptions options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_windows
description: Windows implementation of shared_preferences
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_windows
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.4.0
version: 2.4.1

environment:
sdk: ^3.3.0
Expand Down