[shared_preferences] Move away from shared method channel implementation in native packages. (#4723)
diff --git a/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart
index 1938a04..11498cf 100755
--- a/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart
+++ b/packages/shared_preferences/shared_preferences/test/shared_preferences_test.dart
@@ -47,11 +47,6 @@
store.log.clear();
});
- tearDown(() async {
- await preferences.clear();
- await store.clear();
- });
-
test('reading', () async {
expect(preferences.get('String'), testString);
expect(preferences.get('bool'), testBool);
diff --git a/packages/shared_preferences/shared_preferences_android/CHANGELOG.md b/packages/shared_preferences/shared_preferences_android/CHANGELOG.md
index 527d64d..5321e86 100644
--- a/packages/shared_preferences/shared_preferences_android/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_android/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.0.11
+
+* Switches to an in-package method channel implementation.
+
## 2.0.10
* Removes dependency on `meta`.
diff --git a/packages/shared_preferences/shared_preferences_android/android/src/main/java/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.java b/packages/shared_preferences/shared_preferences_android/android/src/main/java/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.java
index d41328e..9545fe9 100644
--- a/packages/shared_preferences/shared_preferences_android/android/src/main/java/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.java
+++ b/packages/shared_preferences/shared_preferences_android/android/src/main/java/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.java
@@ -11,7 +11,7 @@
/** SharedPreferencesPlugin */
public class SharedPreferencesPlugin implements FlutterPlugin {
- private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences";
+ private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences_android";
private MethodChannel channel;
private MethodCallHandlerImpl handler;
diff --git a/packages/shared_preferences/shared_preferences_android/lib/shared_preferences_android.dart b/packages/shared_preferences/shared_preferences_android/lib/shared_preferences_android.dart
new file mode 100644
index 0000000..86f447b
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_android/lib/shared_preferences_android.dart
@@ -0,0 +1,53 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:async';
+
+import 'package:flutter/services.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+const MethodChannel _kChannel =
+ MethodChannel('plugins.flutter.io/shared_preferences_android');
+
+/// The macOS implementation of [SharedPreferencesStorePlatform].
+///
+/// This class implements the `package:shared_preferences` functionality for Android.
+class SharedPreferencesAndroid extends SharedPreferencesStorePlatform {
+ /// Registers this class as the default instance of [SharedPreferencesStorePlatform].
+ static void registerWith() {
+ SharedPreferencesStorePlatform.instance = SharedPreferencesAndroid();
+ }
+
+ @override
+ Future<bool> remove(String key) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'remove',
+ <String, dynamic>{'key': key},
+ ))!;
+ }
+
+ @override
+ Future<bool> setValue(String valueType, String key, Object value) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'set$valueType',
+ <String, dynamic>{'key': key, 'value': value},
+ ))!;
+ }
+
+ @override
+ Future<bool> clear() async {
+ return (await _kChannel.invokeMethod<bool>('clear'))!;
+ }
+
+ @override
+ Future<Map<String, Object>> getAll() async {
+ final Map<String, Object>? preferences =
+ await _kChannel.invokeMapMethod<String, Object>('getAll');
+
+ if (preferences == null) {
+ return <String, Object>{};
+ }
+ return preferences;
+ }
+}
diff --git a/packages/shared_preferences/shared_preferences_android/pubspec.yaml b/packages/shared_preferences/shared_preferences_android/pubspec.yaml
index 8adb240..7eb180f 100644
--- a/packages/shared_preferences/shared_preferences_android/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_android/pubspec.yaml
@@ -2,11 +2,11 @@
description: Android implementation of the shared_preferences plugin
repository: https://github.com/flutter/plugins/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.0.10
+version: 2.0.11
environment:
sdk: ">=2.14.0 <3.0.0"
- flutter: ">=2.5.0"
+ flutter: ">=2.8.0"
flutter:
plugin:
@@ -15,6 +15,7 @@
android:
package: io.flutter.plugins.sharedpreferences
pluginClass: SharedPreferencesPlugin
+ dartPluginClass: SharedPreferencesAndroid
dependencies:
flutter:
@@ -24,4 +25,3 @@
dev_dependencies:
flutter_test:
sdk: flutter
- pedantic: ^1.10.0
diff --git a/packages/shared_preferences/shared_preferences_android/test/shared_preferences_android_test.dart b/packages/shared_preferences/shared_preferences_android/test/shared_preferences_android_test.dart
new file mode 100644
index 0000000..92d9e8f
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_android/test/shared_preferences_android_test.dart
@@ -0,0 +1,117 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:shared_preferences_android/shared_preferences_android.dart';
+import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ group(MethodChannelSharedPreferencesStore, () {
+ const MethodChannel channel = MethodChannel(
+ 'plugins.flutter.io/shared_preferences_android',
+ );
+
+ const Map<String, Object> kTestValues = <String, Object>{
+ 'flutter.String': 'hello world',
+ 'flutter.Bool': true,
+ 'flutter.Int': 42,
+ 'flutter.Double': 3.14159,
+ 'flutter.StringList': <String>['foo', 'bar'],
+ };
+ // Create a dummy in-memory implementation to back the mocked method channel
+ // API to simplify validation of the expected calls.
+ late InMemorySharedPreferencesStore testData;
+
+ final List<MethodCall> log = <MethodCall>[];
+ late SharedPreferencesStorePlatform store;
+
+ setUp(() async {
+ testData = InMemorySharedPreferencesStore.empty();
+
+ channel.setMockMethodCallHandler((MethodCall methodCall) async {
+ log.add(methodCall);
+ if (methodCall.method == 'getAll') {
+ return await testData.getAll();
+ }
+ if (methodCall.method == 'remove') {
+ final String key = methodCall.arguments['key']! as String;
+ return await testData.remove(key);
+ }
+ if (methodCall.method == 'clear') {
+ return await testData.clear();
+ }
+ final RegExp setterRegExp = RegExp(r'set(.*)');
+ final Match? match = setterRegExp.matchAsPrefix(methodCall.method);
+ if (match?.groupCount == 1) {
+ final String valueType = match!.group(1)!;
+ final String key = methodCall.arguments['key'] as String;
+ final Object value = methodCall.arguments['value'] as Object;
+ return await testData.setValue(valueType, key, value);
+ }
+ fail('Unexpected method call: ${methodCall.method}');
+ });
+ log.clear();
+ });
+
+ test('registered instance', () {
+ SharedPreferencesAndroid.registerWith();
+ expect(SharedPreferencesStorePlatform.instance,
+ isA<SharedPreferencesAndroid>());
+ });
+
+ test('getAll', () async {
+ store = SharedPreferencesAndroid();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.getAll(), kTestValues);
+ expect(log.single.method, 'getAll');
+ });
+
+ test('remove', () async {
+ store = SharedPreferencesAndroid();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.remove('flutter.String'), true);
+ expect(await store.remove('flutter.Bool'), true);
+ expect(await store.remove('flutter.Int'), true);
+ expect(await store.remove('flutter.Double'), true);
+ expect(await testData.getAll(), <String, dynamic>{
+ 'flutter.StringList': <String>['foo', 'bar'],
+ });
+
+ expect(log, hasLength(4));
+ for (final MethodCall call in log) {
+ expect(call.method, 'remove');
+ }
+ });
+
+ test('setValue', () async {
+ store = SharedPreferencesAndroid();
+ expect(await testData.getAll(), isEmpty);
+ for (final String key in kTestValues.keys) {
+ final Object value = kTestValues[key]!;
+ expect(await store.setValue(key.split('.').last, key, value), true);
+ }
+ expect(await testData.getAll(), kTestValues);
+
+ expect(log, hasLength(5));
+ expect(log[0].method, 'setString');
+ expect(log[1].method, 'setBool');
+ expect(log[2].method, 'setInt');
+ expect(log[3].method, 'setDouble');
+ expect(log[4].method, 'setStringList');
+ });
+
+ test('clear', () async {
+ store = SharedPreferencesAndroid();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await testData.getAll(), isNotEmpty);
+ expect(await store.clear(), true);
+ expect(await testData.getAll(), isEmpty);
+ expect(log.single.method, 'clear');
+ });
+ });
+}
diff --git a/packages/shared_preferences/shared_preferences_ios/CHANGELOG.md b/packages/shared_preferences/shared_preferences_ios/CHANGELOG.md
index 84fe4f8..b2a9f14 100644
--- a/packages/shared_preferences/shared_preferences_ios/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_ios/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.0.10
+
+* Switches to an in-package method channel implementation.
+
## 2.0.9
* Removes dependency on `meta`.
diff --git a/packages/shared_preferences/shared_preferences_ios/ios/Classes/FLTSharedPreferencesPlugin.m b/packages/shared_preferences/shared_preferences_ios/ios/Classes/FLTSharedPreferencesPlugin.m
index 09308d4..4d49e3b 100644
--- a/packages/shared_preferences/shared_preferences_ios/ios/Classes/FLTSharedPreferencesPlugin.m
+++ b/packages/shared_preferences/shared_preferences_ios/ios/Classes/FLTSharedPreferencesPlugin.m
@@ -4,7 +4,7 @@
#import "FLTSharedPreferencesPlugin.h"
-static NSString *const CHANNEL_NAME = @"plugins.flutter.io/shared_preferences";
+static NSString *const CHANNEL_NAME = @"plugins.flutter.io/shared_preferences_ios";
@implementation FLTSharedPreferencesPlugin
diff --git a/packages/shared_preferences/shared_preferences_ios/lib/shared_preferences_ios.dart b/packages/shared_preferences/shared_preferences_ios/lib/shared_preferences_ios.dart
new file mode 100644
index 0000000..15f1e2f
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_ios/lib/shared_preferences_ios.dart
@@ -0,0 +1,53 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:async';
+
+import 'package:flutter/services.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+const MethodChannel _kChannel =
+ MethodChannel('plugins.flutter.io/shared_preferences_ios');
+
+/// The macOS implementation of [SharedPreferencesStorePlatform].
+///
+/// This class implements the `package:shared_preferences` functionality for iOS.
+class SharedPreferencesIOS extends SharedPreferencesStorePlatform {
+ /// Registers this class as the default instance of [SharedPreferencesStorePlatform].
+ static void registerWith() {
+ SharedPreferencesStorePlatform.instance = SharedPreferencesIOS();
+ }
+
+ @override
+ Future<bool> remove(String key) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'remove',
+ <String, dynamic>{'key': key},
+ ))!;
+ }
+
+ @override
+ Future<bool> setValue(String valueType, String key, Object value) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'set$valueType',
+ <String, dynamic>{'key': key, 'value': value},
+ ))!;
+ }
+
+ @override
+ Future<bool> clear() async {
+ return (await _kChannel.invokeMethod<bool>('clear'))!;
+ }
+
+ @override
+ Future<Map<String, Object>> getAll() async {
+ final Map<String, Object>? preferences =
+ await _kChannel.invokeMapMethod<String, Object>('getAll');
+
+ if (preferences == null) {
+ return <String, Object>{};
+ }
+ return preferences;
+ }
+}
diff --git a/packages/shared_preferences/shared_preferences_ios/pubspec.yaml b/packages/shared_preferences/shared_preferences_ios/pubspec.yaml
index ad3f20b..68ab035 100644
--- a/packages/shared_preferences/shared_preferences_ios/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_ios/pubspec.yaml
@@ -2,11 +2,11 @@
description: iOS implementation of the shared_preferences plugin
repository: https://github.com/flutter/plugins/tree/main/packages/shared_preferences/shared_preferences_ios
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
-version: 2.0.9
+version: 2.0.10
environment:
sdk: ">=2.14.0 <3.0.0"
- flutter: ">=2.5.0"
+ flutter: ">=2.8.0"
flutter:
plugin:
@@ -14,6 +14,7 @@
platforms:
ios:
pluginClass: FLTSharedPreferencesPlugin
+ dartPluginClass: SharedPreferencesIOS
dependencies:
flutter:
@@ -23,4 +24,3 @@
dev_dependencies:
flutter_test:
sdk: flutter
- pedantic: ^1.10.0
diff --git a/packages/shared_preferences/shared_preferences_ios/test/shared_preferences_ios_test.dart b/packages/shared_preferences/shared_preferences_ios/test/shared_preferences_ios_test.dart
new file mode 100644
index 0000000..8eb23f2
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_ios/test/shared_preferences_ios_test.dart
@@ -0,0 +1,117 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:shared_preferences_ios/shared_preferences_ios.dart';
+import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ group(MethodChannelSharedPreferencesStore, () {
+ const MethodChannel channel = MethodChannel(
+ 'plugins.flutter.io/shared_preferences_ios',
+ );
+
+ const Map<String, Object> kTestValues = <String, Object>{
+ 'flutter.String': 'hello world',
+ 'flutter.Bool': true,
+ 'flutter.Int': 42,
+ 'flutter.Double': 3.14159,
+ 'flutter.StringList': <String>['foo', 'bar'],
+ };
+ // Create a dummy in-memory implementation to back the mocked method channel
+ // API to simplify validation of the expected calls.
+ late InMemorySharedPreferencesStore testData;
+
+ final List<MethodCall> log = <MethodCall>[];
+ late SharedPreferencesStorePlatform store;
+
+ setUp(() async {
+ testData = InMemorySharedPreferencesStore.empty();
+
+ channel.setMockMethodCallHandler((MethodCall methodCall) async {
+ log.add(methodCall);
+ if (methodCall.method == 'getAll') {
+ return await testData.getAll();
+ }
+ if (methodCall.method == 'remove') {
+ final String key = methodCall.arguments['key'] as String;
+ return await testData.remove(key);
+ }
+ if (methodCall.method == 'clear') {
+ return await testData.clear();
+ }
+ final RegExp setterRegExp = RegExp(r'set(.*)');
+ final Match? match = setterRegExp.matchAsPrefix(methodCall.method);
+ if (match?.groupCount == 1) {
+ final String valueType = match!.group(1)!;
+ final String key = methodCall.arguments['key'] as String;
+ final Object value = methodCall.arguments['value'] as Object;
+ return await testData.setValue(valueType, key, value);
+ }
+ fail('Unexpected method call: ${methodCall.method}');
+ });
+ log.clear();
+ });
+
+ test('registered instance', () {
+ SharedPreferencesIOS.registerWith();
+ expect(
+ SharedPreferencesStorePlatform.instance, isA<SharedPreferencesIOS>());
+ });
+
+ test('getAll', () async {
+ store = SharedPreferencesIOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.getAll(), kTestValues);
+ expect(log.single.method, 'getAll');
+ });
+
+ test('remove', () async {
+ store = SharedPreferencesIOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.remove('flutter.String'), true);
+ expect(await store.remove('flutter.Bool'), true);
+ expect(await store.remove('flutter.Int'), true);
+ expect(await store.remove('flutter.Double'), true);
+ expect(await testData.getAll(), <String, dynamic>{
+ 'flutter.StringList': <String>['foo', 'bar'],
+ });
+
+ expect(log, hasLength(4));
+ for (final MethodCall call in log) {
+ expect(call.method, 'remove');
+ }
+ });
+
+ test('setValue', () async {
+ store = SharedPreferencesIOS();
+ expect(await testData.getAll(), isEmpty);
+ for (final String key in kTestValues.keys) {
+ final Object value = kTestValues[key]!;
+ expect(await store.setValue(key.split('.').last, key, value), true);
+ }
+ expect(await testData.getAll(), kTestValues);
+
+ expect(log, hasLength(5));
+ expect(log[0].method, 'setString');
+ expect(log[1].method, 'setBool');
+ expect(log[2].method, 'setInt');
+ expect(log[3].method, 'setDouble');
+ expect(log[4].method, 'setStringList');
+ });
+
+ test('clear', () async {
+ store = SharedPreferencesIOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await testData.getAll(), isNotEmpty);
+ expect(await store.clear(), true);
+ expect(await testData.getAll(), isEmpty);
+ expect(log.single.method, 'clear');
+ });
+ });
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
index bfc48b7..7c86b3c 100644
--- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.1.0
+
+* Deprecated `SharedPreferencesWindows.instance` in favor of `SharedPreferencesStorePlatform.instance`.
+
## 2.0.4
* Removes dependency on `meta`.
diff --git a/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart
index b33d601..1d83eea 100644
--- a/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart
+++ b/packages/shared_preferences/shared_preferences_linux/example/integration_test/shared_preferences_test.dart
@@ -31,7 +31,7 @@
late SharedPreferencesLinux preferences;
setUp(() async {
- preferences = SharedPreferencesLinux.instance;
+ preferences = SharedPreferencesLinux();
});
tearDown(() {
diff --git a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart
index b4a7ea3..4b71c7e 100644
--- a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart
+++ b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart
@@ -31,7 +31,7 @@
}
class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
- final SharedPreferencesLinux prefs = SharedPreferencesLinux.instance;
+ final SharedPreferencesLinux prefs = SharedPreferencesLinux();
late Future<int> _counter;
Future<void> _incrementCounter() async {
diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
index d6dcceb..b6a9a5b 100644
--- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
+++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
@@ -16,14 +16,14 @@
///
/// This class implements the `package:shared_preferences` functionality for Linux.
class SharedPreferencesLinux extends SharedPreferencesStorePlatform {
- /// The default instance of [SharedPreferencesLinux] to use.
- // TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable.
- // https://github.com/flutter/flutter/issues/81421
+ /// Deprecated instance of [SharedPreferencesLinux].
+ /// Use [SharedPreferencesStorePlatform.instance] instead.
+ @Deprecated('Use `SharedPreferencesStorePlatform.instance` instead.')
static SharedPreferencesLinux instance = SharedPreferencesLinux();
/// Registers the Linux implementation.
static void registerWith() {
- SharedPreferencesStorePlatform.instance = instance;
+ SharedPreferencesStorePlatform.instance = SharedPreferencesLinux();
}
/// Local copy of preferences
@@ -33,9 +33,12 @@
@visibleForTesting
FileSystem fs = const LocalFileSystem();
+ /// The path_provider_linux instance used to find the support directory.
+ @visibleForTesting
+ PathProviderLinux pathProvider = PathProviderLinux();
+
/// Gets the file where the preferences are stored.
Future<File?> _getLocalDataFile() async {
- final PathProviderLinux pathProvider = PathProviderLinux();
final String? directory = await pathProvider.getApplicationSupportPath();
if (directory == null) {
return null;
diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
index 1b84644..8ab692a 100644
--- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
@@ -2,11 +2,11 @@
description: Linux implementation of the shared_preferences plugin
repository: https://github.com/flutter/plugins/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.0.4
+version: 2.1.0
environment:
sdk: ">=2.12.0 <3.0.0"
- flutter: ">=2.0.0"
+ flutter: ">=2.5.0"
flutter:
plugin:
@@ -21,9 +21,9 @@
sdk: flutter
path: ^1.8.0
path_provider_linux: ^2.0.0
+ path_provider_platform_interface: ^2.0.0
shared_preferences_platform_interface: ^2.0.0
dev_dependencies:
flutter_test:
sdk: flutter
- pedantic: ^1.10.0
diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
index 40dbdd5..57acd17 100644
--- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
+++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
@@ -5,20 +5,22 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_linux/path_provider_linux.dart';
+import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:shared_preferences_linux/shared_preferences_linux.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
void main() {
late MemoryFileSystem fs;
+ late PathProviderLinux pathProvider;
SharedPreferencesLinux.registerWith();
setUp(() {
fs = MemoryFileSystem.test();
+ pathProvider = FakePathProviderLinux();
});
Future<String> _getFilePath() async {
- final PathProviderLinux pathProvider = PathProviderLinux();
final String? directory = await pathProvider.getApplicationSupportPath();
return path.join(directory!, 'shared_preferences.json');
}
@@ -36,10 +38,12 @@
SharedPreferencesLinux _getPreferences() {
final SharedPreferencesLinux prefs = SharedPreferencesLinux();
prefs.fs = fs;
+ prefs.pathProvider = pathProvider;
return prefs;
}
test('registered instance', () {
+ SharedPreferencesLinux.registerWith();
expect(
SharedPreferencesStorePlatform.instance, isA<SharedPreferencesLinux>());
});
@@ -81,3 +85,26 @@
expect(await _readTestFile(), '{}');
});
}
+
+/// Fake implementation of PathProviderLinux that returns hard-coded paths,
+/// allowing tests to run on any platform.
+///
+/// Note that this should only be used with an in-memory filesystem, as the
+/// path it returns is a root path that does not actually exist on Linux.
+class FakePathProviderLinux extends PathProviderPlatform
+ implements PathProviderLinux {
+ @override
+ Future<String?> getApplicationSupportPath() async => r'/appsupport';
+
+ @override
+ Future<String?> getTemporaryPath() async => null;
+
+ @override
+ Future<String?> getLibraryPath() async => null;
+
+ @override
+ Future<String?> getApplicationDocumentsPath() async => null;
+
+ @override
+ Future<String?> getDownloadsPath() async => null;
+}
diff --git a/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md b/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md
index 7a2e3ab..1f586a2 100644
--- a/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_macos/CHANGELOG.md
@@ -1,5 +1,6 @@
-## NEXT
+## 2.0.3
+* Switches to an in-package method channel implementation.
* Fixes newly enabled analyzer options.
## 2.0.2
diff --git a/packages/shared_preferences/shared_preferences_macos/lib/shared_preferences_macos.dart b/packages/shared_preferences/shared_preferences_macos/lib/shared_preferences_macos.dart
new file mode 100644
index 0000000..a97fe13
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_macos/lib/shared_preferences_macos.dart
@@ -0,0 +1,53 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'dart:async';
+
+import 'package:flutter/services.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+const MethodChannel _kChannel =
+ MethodChannel('plugins.flutter.io/shared_preferences_macos');
+
+/// The macOS implementation of [SharedPreferencesStorePlatform].
+///
+/// This class implements the `package:shared_preferences` functionality for macOS.
+class SharedPreferencesMacOS extends SharedPreferencesStorePlatform {
+ /// Registers this class as the default instance of [SharedPreferencesStorePlatform].
+ static void registerWith() {
+ SharedPreferencesStorePlatform.instance = SharedPreferencesMacOS();
+ }
+
+ @override
+ Future<bool> remove(String key) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'remove',
+ <String, dynamic>{'key': key},
+ ))!;
+ }
+
+ @override
+ Future<bool> setValue(String valueType, String key, Object value) async {
+ return (await _kChannel.invokeMethod<bool>(
+ 'set$valueType',
+ <String, dynamic>{'key': key, 'value': value},
+ ))!;
+ }
+
+ @override
+ Future<bool> clear() async {
+ return (await _kChannel.invokeMethod<bool>('clear'))!;
+ }
+
+ @override
+ Future<Map<String, Object>> getAll() async {
+ final Map<String, Object>? preferences =
+ await _kChannel.invokeMapMethod<String, Object>('getAll');
+
+ if (preferences == null) {
+ return <String, Object>{};
+ }
+ return preferences;
+ }
+}
diff --git a/packages/shared_preferences/shared_preferences_macos/macos/Classes/SharedPreferencesPlugin.swift b/packages/shared_preferences/shared_preferences_macos/macos/Classes/SharedPreferencesPlugin.swift
index 2cf345c..91b4244 100644
--- a/packages/shared_preferences/shared_preferences_macos/macos/Classes/SharedPreferencesPlugin.swift
+++ b/packages/shared_preferences/shared_preferences_macos/macos/Classes/SharedPreferencesPlugin.swift
@@ -8,7 +8,7 @@
public class SharedPreferencesPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
- name: "plugins.flutter.io/shared_preferences",
+ name: "plugins.flutter.io/shared_preferences_macos",
binaryMessenger: registrar.messenger)
let instance = SharedPreferencesPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
diff --git a/packages/shared_preferences/shared_preferences_macos/pubspec.yaml b/packages/shared_preferences/shared_preferences_macos/pubspec.yaml
index 4f06c52..0873696 100644
--- a/packages/shared_preferences/shared_preferences_macos/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_macos/pubspec.yaml
@@ -2,11 +2,11 @@
description: macOS implementation of the shared_preferences plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/shared_preferences/shared_preferences_macos
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
-version: 2.0.2
+version: 2.0.3
environment:
sdk: ">=2.12.0 <3.0.0"
- flutter: ">=2.0.0"
+ flutter: ">=2.5.0"
flutter:
plugin:
@@ -14,6 +14,7 @@
platforms:
macos:
pluginClass: SharedPreferencesPlugin
+ dartPluginClass: SharedPreferencesMacOS
dependencies:
flutter:
@@ -21,4 +22,5 @@
shared_preferences_platform_interface: ^2.0.0
dev_dependencies:
- pedantic: ^1.10.0
+ flutter_test:
+ sdk: flutter
diff --git a/packages/shared_preferences/shared_preferences_macos/test/shared_preferences_macos_test.dart b/packages/shared_preferences/shared_preferences_macos/test/shared_preferences_macos_test.dart
new file mode 100644
index 0000000..cd858f4
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_macos/test/shared_preferences_macos_test.dart
@@ -0,0 +1,117 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:shared_preferences_macos/shared_preferences_macos.dart';
+import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ group(MethodChannelSharedPreferencesStore, () {
+ const MethodChannel channel = MethodChannel(
+ 'plugins.flutter.io/shared_preferences_macos',
+ );
+
+ const Map<String, Object> kTestValues = <String, Object>{
+ 'flutter.String': 'hello world',
+ 'flutter.Bool': true,
+ 'flutter.Int': 42,
+ 'flutter.Double': 3.14159,
+ 'flutter.StringList': <String>['foo', 'bar'],
+ };
+ // Create a dummy in-memory implementation to back the mocked method channel
+ // API to simplify validation of the expected calls.
+ late InMemorySharedPreferencesStore testData;
+
+ final List<MethodCall> log = <MethodCall>[];
+ late SharedPreferencesStorePlatform store;
+
+ setUp(() async {
+ testData = InMemorySharedPreferencesStore.empty();
+
+ channel.setMockMethodCallHandler((MethodCall methodCall) async {
+ log.add(methodCall);
+ if (methodCall.method == 'getAll') {
+ return await testData.getAll();
+ }
+ if (methodCall.method == 'remove') {
+ final String key = (methodCall.arguments['key'] as String?)!;
+ return await testData.remove(key);
+ }
+ if (methodCall.method == 'clear') {
+ return await testData.clear();
+ }
+ final RegExp setterRegExp = RegExp(r'set(.*)');
+ final Match? match = setterRegExp.matchAsPrefix(methodCall.method);
+ if (match?.groupCount == 1) {
+ final String valueType = match!.group(1)!;
+ final String key = (methodCall.arguments['key'] as String?)!;
+ final Object value = (methodCall.arguments['value'] as Object?)!;
+ return await testData.setValue(valueType, key, value);
+ }
+ fail('Unexpected method call: ${methodCall.method}');
+ });
+ log.clear();
+ });
+
+ test('registers instance', () {
+ SharedPreferencesMacOS.registerWith();
+ expect(SharedPreferencesStorePlatform.instance,
+ isA<SharedPreferencesMacOS>());
+ });
+
+ test('getAll', () async {
+ store = SharedPreferencesMacOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.getAll(), kTestValues);
+ expect(log.single.method, 'getAll');
+ });
+
+ test('remove', () async {
+ store = SharedPreferencesMacOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await store.remove('flutter.String'), true);
+ expect(await store.remove('flutter.Bool'), true);
+ expect(await store.remove('flutter.Int'), true);
+ expect(await store.remove('flutter.Double'), true);
+ expect(await testData.getAll(), <String, dynamic>{
+ 'flutter.StringList': <String>['foo', 'bar'],
+ });
+
+ expect(log, hasLength(4));
+ for (final MethodCall call in log) {
+ expect(call.method, 'remove');
+ }
+ });
+
+ test('setValue', () async {
+ store = SharedPreferencesMacOS();
+ expect(await testData.getAll(), isEmpty);
+ for (final String key in kTestValues.keys) {
+ final Object value = kTestValues[key]!;
+ expect(await store.setValue(key.split('.').last, key, value), true);
+ }
+ expect(await testData.getAll(), kTestValues);
+
+ expect(log, hasLength(5));
+ expect(log[0].method, 'setString');
+ expect(log[1].method, 'setBool');
+ expect(log[2].method, 'setInt');
+ expect(log[3].method, 'setDouble');
+ expect(log[4].method, 'setStringList');
+ });
+
+ test('clear', () async {
+ store = SharedPreferencesMacOS();
+ testData = InMemorySharedPreferencesStore.withData(kTestValues);
+ expect(await testData.getAll(), isNotEmpty);
+ expect(await store.clear(), true);
+ expect(await testData.getAll(), isEmpty);
+ expect(log.single.method, 'clear');
+ });
+ });
+}
diff --git a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md
index 18ae9d1..6c96681 100644
--- a/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_windows/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.1.0
+
+* Deprecated `SharedPreferencesWindows.instance` in favor of `SharedPreferencesStorePlatform.instance`.
+
## 2.0.4
* Removes dependency on `meta`.
diff --git a/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart
index ac13866..92a34fc 100644
--- a/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart
+++ b/packages/shared_preferences/shared_preferences_windows/example/integration_test/shared_preferences_test.dart
@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-import 'dart:async';
-
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';
@@ -28,17 +26,9 @@
'flutter.List': <String>['baz', 'quox'],
};
- late SharedPreferencesWindows preferences;
-
- setUp(() async {
- preferences = SharedPreferencesWindows.instance;
- });
-
- tearDown(() {
- preferences.clear();
- });
-
testWidgets('reading', (WidgetTester _) async {
+ final SharedPreferencesWindows preferences = SharedPreferencesWindows();
+ preferences.clear();
final Map<String, Object> values = await preferences.getAll();
expect(values['String'], isNull);
expect(values['bool'], isNull);
@@ -48,16 +38,16 @@
});
testWidgets('writing', (WidgetTester _) async {
- await Future.wait(<Future<bool>>[
- preferences.setValue(
- 'String', 'String', kTestValues2['flutter.String']!),
- preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']!),
- preferences.setValue('Int', 'int', kTestValues2['flutter.int']!),
- preferences.setValue(
- 'Double', 'double', kTestValues2['flutter.double']!),
- preferences.setValue(
- 'StringList', 'List', kTestValues2['flutter.List']!)
- ]);
+ final SharedPreferencesWindows preferences = SharedPreferencesWindows();
+ preferences.clear();
+ await preferences.setValue(
+ 'String', 'String', kTestValues2['flutter.String']!);
+ await preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']!);
+ await preferences.setValue('Int', 'int', kTestValues2['flutter.int']!);
+ await preferences.setValue(
+ 'Double', 'double', kTestValues2['flutter.double']!);
+ await preferences.setValue(
+ 'StringList', 'List', kTestValues2['flutter.List']!);
final Map<String, Object> values = await preferences.getAll();
expect(values['String'], kTestValues2['flutter.String']);
expect(values['bool'], kTestValues2['flutter.bool']);
@@ -67,6 +57,8 @@
});
testWidgets('removing', (WidgetTester _) async {
+ final SharedPreferencesWindows preferences = SharedPreferencesWindows();
+ preferences.clear();
const String key = 'testKey';
await preferences.setValue('String', key, kTestValues['flutter.String']!);
await preferences.setValue('Bool', key, kTestValues['flutter.bool']!);
@@ -80,6 +72,8 @@
});
testWidgets('clearing', (WidgetTester _) async {
+ final SharedPreferencesWindows preferences = SharedPreferencesWindows();
+ preferences.clear();
await preferences.setValue(
'String', 'String', kTestValues['flutter.String']!);
await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']!);
diff --git a/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart b/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart
index 423a727..40a9159 100644
--- a/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart
+++ b/packages/shared_preferences/shared_preferences_windows/example/lib/main.dart
@@ -31,7 +31,7 @@
}
class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
- final SharedPreferencesWindows prefs = SharedPreferencesWindows.instance;
+ final SharedPreferencesWindows prefs = SharedPreferencesWindows();
late Future<int> _counter;
Future<void> _incrementCounter() async {
diff --git a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart
index e3551d2..a60d2ed 100644
--- a/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart
+++ b/packages/shared_preferences/shared_preferences_windows/lib/shared_preferences_windows.dart
@@ -16,14 +16,14 @@
///
/// This class implements the `package:shared_preferences` functionality for Windows.
class SharedPreferencesWindows extends SharedPreferencesStorePlatform {
- /// The default instance of [SharedPreferencesWindows] to use.
- // TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable.
- // https://github.com/flutter/flutter/issues/81421
+ /// Deprecated instance of [SharedPreferencesWindows].
+ /// Use [SharedPreferencesStorePlatform.instance] instead.
+ @Deprecated('Use `SharedPreferencesStorePlatform.instance` instead.')
static SharedPreferencesWindows instance = SharedPreferencesWindows();
/// Registers the Windows implementation.
static void registerWith() {
- SharedPreferencesStorePlatform.instance = instance;
+ SharedPreferencesStorePlatform.instance = SharedPreferencesWindows();
}
/// File system used to store to disk. Exposed for testing only.
diff --git a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml
index a3938ba..6dcb599 100644
--- a/packages/shared_preferences/shared_preferences_windows/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_windows/pubspec.yaml
@@ -2,11 +2,11 @@
description: Windows implementation of shared_preferences
repository: https://github.com/flutter/plugins/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.0.4
+version: 2.1.0
environment:
sdk: '>=2.12.0 <3.0.0'
- flutter: ">=2.0.0"
+ flutter: ">=2.5.0"
flutter:
plugin:
@@ -27,4 +27,3 @@
dev_dependencies:
flutter_test:
sdk: flutter
- pedantic: ^1.10.0