Skip to content

[shared_preferences] Fix JSON parsing issue with _decodeValue #8211

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 6 commits into from
Feb 18, 2025
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.4.3

* Fixes issue where non-JSON formatted strings cause parsing errors.

## 2.4.2

* Fixes `getStringList` returning immutable list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ void main() {
);
expect(values['Int'], writeCount);
});

testWidgets('returns all valid JSON data', (WidgetTester _) async {
const String value = 'value';
const String invalidJsonDataKey = 'invalidJsonData';
const String validJsonDataKey = 'validJsonData';
html.window.localStorage.setItem(invalidJsonDataKey, value);
html.window.localStorage.setItem(validJsonDataKey, '"$value"');

final Map<String, Object> values = await preferences.getAllWithParameters(
GetAllParameters(
filter: PreferencesFilter(prefix: ''),
),
);

expect(values.containsKey(invalidJsonDataKey), isFalse);
expect(values.containsKey(validJsonDataKey), isTrue);
expect(values[validJsonDataKey], equals(value));
});
});

group('shared_preferences_async', () {
Expand Down Expand Up @@ -454,6 +472,29 @@ void main() {
expect(list?.length, testList.length + 1);
});

testWidgets(
'returns null when reading invalid JSON value',
(WidgetTester _) async {
const String value = 'value';
const String invalidJsonDataKey = 'invalidJsonData';
const String validJsonDataKey = 'validJsonData';
final SharedPreferencesAsyncPlatform preferences =
await getPreferences();

html.window.localStorage.setItem(invalidJsonDataKey, value);
html.window.localStorage.setItem(validJsonDataKey, '"$value"');

expect(
await preferences.getString(invalidJsonDataKey, emptyOptions),
isNull,
);
expect(
await preferences.getString(validJsonDataKey, emptyOptions),
equals(value),
);
},
);

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 @@ -73,7 +73,11 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
final Map<String, Object> allData = <String, Object>{};
for (final String key
in _getPrefixedKeys(filter.prefix, allowList: filter.allowList)) {
allData[key] = _decodeValue(html.window.localStorage.getItem(key)!);
final Object? value =
_decodeValue(html.window.localStorage.getItem(key)!);
if (value != null) {
allData[key] = value;
}
}
return allData;
}
Expand Down Expand Up @@ -132,7 +136,11 @@ base class SharedPreferencesAsyncWeb extends SharedPreferencesAsyncPlatform {
) async {
final Map<String, Object> allData = <String, Object>{};
for (final String key in _getAllowedKeys(allowList: allowList)) {
allData[key] = _decodeValue(html.window.localStorage.getItem(key)!);
final Object? value =
_decodeValue(html.window.localStorage.getItem(key)!);
if (value != null) {
allData[key] = value;
}
}
return allData;
}
Expand Down Expand Up @@ -258,8 +266,13 @@ String _encodeValue(Object? value) {
return json.encode(value);
}

Object _decodeValue(String encodedValue) {
final Object? decodedValue = json.decode(encodedValue);
Object? _decodeValue(String encodedValue) {
final Object? decodedValue;
try {
decodedValue = json.decode(encodedValue);
} on FormatException catch (_) {
return null;
}

if (decodedValue is List) {
// JSON does not preserve generics. The encode/decode roundtrip is
Expand All @@ -268,7 +281,7 @@ Object _decodeValue(String encodedValue) {
return decodedValue.cast<String>();
}

return decodedValue!;
return decodedValue;
}

/// Web specific SharedPreferences Options.
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.2
version: 2.4.3

environment:
sdk: ^3.4.0
Expand Down
Loading