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

Implement shared_preferences on top of platform interface #2325

Merged
merged 6 commits into from Nov 27, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -30,6 +30,7 @@ keystore.properties
gradlew
gradlew.bat
gradle-wrapper.jar
.flutter-plugins-dependencies
*.iml

GeneratedPluginRegistrant.h
Expand Down
11 changes: 0 additions & 11 deletions packages/shared_preferences/analysis_options.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
@@ -1,3 +1,8 @@
## 0.5.4+8

* Switch `package:shared_preferences` to `package:shared_preferences_platform_interface`.
No code changes are necessary in Flutter apps. This is not a breaking change.

## 0.5.4+7

* Restructure the project for Web support.
Expand Down
Expand Up @@ -67,13 +67,14 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
case ConnectionState.waiting:
return const CircularProgressIndicator();
default:
if (snapshot.hasError)
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
else
} else {
return Text(
'Button tapped ${snapshot.data} time${snapshot.data == 1 ? '' : 's'}.\n\n'
'This should persist across restarts.',
);
}
}
})),
floatingActionButton: FloatingActionButton(
Expand Down
Expand Up @@ -63,23 +63,21 @@ void main() {

test('removing', () async {
const String key = 'testKey';
preferences
..setString(key, kTestValues['flutter.String'])
..setBool(key, kTestValues['flutter.bool'])
..setInt(key, kTestValues['flutter.int'])
..setDouble(key, kTestValues['flutter.double'])
..setStringList(key, kTestValues['flutter.List']);
await preferences.setString(key, kTestValues['flutter.String']);
await preferences.setBool(key, kTestValues['flutter.bool']);
await preferences.setInt(key, kTestValues['flutter.int']);
await preferences.setDouble(key, kTestValues['flutter.double']);
await preferences.setStringList(key, kTestValues['flutter.List']);
await preferences.remove(key);
expect(preferences.get('testKey'), isNull);
});

test('clearing', () async {
preferences
..setString('String', kTestValues['flutter.String'])
..setBool('bool', kTestValues['flutter.bool'])
..setInt('int', kTestValues['flutter.int'])
..setDouble('double', kTestValues['flutter.double'])
..setStringList('List', kTestValues['flutter.List']);
await preferences.setString('String', kTestValues['flutter.String']);
await preferences.setBool('bool', kTestValues['flutter.bool']);
await preferences.setInt('int', kTestValues['flutter.int']);
await preferences.setDouble('double', kTestValues['flutter.double']);
await preferences.setStringList('List', kTestValues['flutter.List']);
await preferences.clear();
expect(preferences.getString('String'), null);
expect(preferences.getBool('bool'), null);
Expand Down
Expand Up @@ -10,6 +10,6 @@ Future<void> main() async {
final FlutterDriver driver = await FlutterDriver.connect();
final String result =
await driver.requestData(null, timeout: const Duration(minutes: 1));
driver.close();
await driver.close();
exit(result == 'pass' ? 0 : 1);
}
Expand Up @@ -4,11 +4,9 @@

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

const MethodChannel _kChannel =
MethodChannel('plugins.flutter.io/shared_preferences');
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';

/// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
/// a persistent store for simple data.
Expand All @@ -20,6 +18,9 @@ class SharedPreferences {
static const String _prefix = 'flutter.';
static Completer<SharedPreferences> _completer;

static SharedPreferencesStorePlatform get _store =>
SharedPreferencesStorePlatform.instance;

/// Loads and parses the [SharedPreferences] for this app from disk.
///
/// Because this is reading from disk, it shouldn't be awaited in
Expand Down Expand Up @@ -124,37 +125,30 @@ class SharedPreferences {
Future<bool> remove(String key) => _setValue(null, key, null);

Future<bool> _setValue(String valueType, String key, Object value) {
final Map<String, dynamic> params = <String, dynamic>{
'key': '$_prefix$key',
};
final String prefixedKey = '$_prefix$key';
if (value == null) {
_preferenceCache.remove(key);
return _kChannel
.invokeMethod<bool>('remove', params)
.then<bool>((dynamic result) => result);
return _store.remove(prefixedKey);
} else {
if (value is List<String>) {
// Make a copy of the list so that later mutations won't propagate
_preferenceCache[key] = value.toList();
} else {
_preferenceCache[key] = value;
}
params['value'] = value;
return _kChannel
.invokeMethod<bool>('set$valueType', params)
.then<bool>((dynamic result) => result);
return _store.setValue(valueType, prefixedKey, value);
}
}

/// Always returns true.
/// On iOS, synchronize is marked deprecated. On Android, we commit every set.
@deprecated
Future<bool> commit() async => await _kChannel.invokeMethod<bool>('commit');
Future<bool> commit() async => true;

/// Completes with true once the user preferences for the app has been cleared.
Future<bool> clear() async {
Future<bool> clear() {
_preferenceCache.clear();
return await _kChannel.invokeMethod<bool>('clear');
return _store.clear();
}

/// Fetches the latest values from the host platform.
Expand All @@ -169,8 +163,7 @@ class SharedPreferences {
}

static Future<Map<String, Object>> _getSharedPreferencesMap() async {
final Map<String, Object> fromSystem =
await _kChannel.invokeMapMethod<String, Object>('getAll');
final Map<String, Object> fromSystem = await _store.getAll();
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
Expand All @@ -194,12 +187,8 @@ class SharedPreferences {
}
return MapEntry<String, dynamic>(newKey, value);
});
_kChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return newValues;
}
return null;
});
SharedPreferencesStorePlatform.instance =
InMemorySharedPreferencesStore.withData(newValues);
_completer = null;
}
}
3 changes: 2 additions & 1 deletion packages/shared_preferences/shared_preferences/pubspec.yaml
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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
version: 0.5.4+7
version: 0.5.4+8

flutter:
plugin:
Expand All @@ -15,6 +15,7 @@ dependencies:
meta: ^1.0.4
flutter:
sdk: flutter
shared_preferences_platform_interface: ^1.0.0

dev_dependencies:
flutter_test:
Expand Down