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

[shared_preferences] Added reload method #1198

Merged
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/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.3

* Add reload method.

## 0.5.2+2

* Updated Gradle tooling to match Android Studio 3.4.
Expand Down
38 changes: 26 additions & 12 deletions packages/shared_preferences/lib/shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ class SharedPreferences {
static SharedPreferences _instance;
static Future<SharedPreferences> getInstance() async {
if (_instance == null) {
final Map<Object, Object> fromSystem =
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
await _kChannel.invokeMethod('getAll');
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
for (String key in fromSystem.keys) {
assert(key.startsWith(_prefix));
preferencesMap[key.substring(_prefix.length)] = fromSystem[key];
}
final Map<String, Object> preferencesMap =
await _getSharedPreferencesMap();
_instance = SharedPreferences._(preferencesMap);
}
return _instance;
Expand Down Expand Up @@ -158,6 +148,30 @@ class SharedPreferences {
return await _kChannel.invokeMethod('clear');
}

/// Fetches the latest values from the host platform.
///
/// Use this method to observe modifications that were made in native code
/// (without using the plugin) while the app is running.
Future<void> reload() async {
final Map<String, Object> preferences =
await SharedPreferences._getSharedPreferencesMap();
_preferenceCache.clear();
_preferenceCache.addAll(preferences);
}

static Future<Map<String, Object>> _getSharedPreferencesMap() async {
final Map<Object, Object> fromSystem =
await _kChannel.invokeMethod('getAll');
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
for (String key in fromSystem.keys) {
assert(key.startsWith(_prefix));
preferencesMap[key.substring(_prefix.length)] = fromSystem[key];
}
return preferencesMap;
}

/// Initializes the shared preferences with mock values for testing.
@visibleForTesting
static void setMockInitialValues(Map<String, dynamic> values) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
version: 0.5.2+2
version: 0.5.3

flutter:
plugin:
Expand Down
11 changes: 11 additions & 0 deletions packages/shared_preferences/test/shared_preferences_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ void main() {
expect(log, <Matcher>[isMethodCall('clear', arguments: null)]);
});

test('reloading', () async {
await preferences.setString('String', kTestValues['flutter.String']);
expect(preferences.getString('String'), kTestValues['flutter.String']);

SharedPreferences.setMockInitialValues(kTestValues2);
expect(preferences.getString('String'), kTestValues['flutter.String']);

await preferences.reload();
expect(preferences.getString('String'), kTestValues2['flutter.String']);
});

test('mocking', () async {
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
Expand Down