Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] fix crash when list type is dynaimc #3565

Merged
merged 4 commits into from
Feb 18, 2021
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
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.0.0-nullsafety.1

* Fix crash when list string's type is dynamic.

## 2.0.0-nullsafety

* Migrate to null-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SharedPreferences {
/// Reads a set of string values from persistent storage, throwing an
/// exception if it's not a string set.
List<String>? getStringList(String key) {
List<Object>? list = _preferenceCache[key] as List<Object>?;
List<dynamic>? list = _preferenceCache[key] as List<dynamic>?;
if (list != null && list is! List<String>) {
list = list.cast<String>().toList();
_preferenceCache[key] = list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences
description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
version: 2.0.0-nullsafety
version: 2.0.0-nullsafety.1

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void main() {

tearDown(() async {
await preferences.clear();
await store.clear();
});

test('reading', () async {
Expand Down Expand Up @@ -156,6 +157,15 @@ void main() {
expect(await first, await second);
});

test('string list type is dynamic (usually from method channel)', () async {
SharedPreferences.setMockInitialValues(<String, Object>{
'dynamic_list': <dynamic>['1', '2']
});
final SharedPreferences prefs = await SharedPreferences.getInstance();
final List<String>? value = prefs.getStringList('dynamic_list');
expect(value, <String>['1', '2']);
});

group('mocking', () {
const String _key = 'dummy';
const String _prefixedKey = 'flutter.' + _key;
Expand Down