Skip to content
Open
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
29 changes: 18 additions & 11 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,7 +21,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -99,7 +99,14 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand All @@ -113,7 +120,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.0.4"
version: "2.0.5"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -169,7 +176,7 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
version: "2.0.13"
shared_preferences_android:
dependency: transitive
description:
Expand All @@ -190,14 +197,14 @@ packages:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.1.0"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
shared_preferences_platform_interface:
dependency: transitive
description:
Expand All @@ -211,14 +218,14 @@ packages:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.1.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -265,7 +272,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
version: "0.4.8"
typed_data:
dependency: transitive
description:
Expand All @@ -279,7 +286,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
win32:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.0.9
shared_preferences: ^2.0.13

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
30 changes: 23 additions & 7 deletions lib/native_shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:meta/meta.dart';
import 'package:native_shared_preferences/native_shared_preferences_platform_interface.dart';
import 'package:shared_preferences_linux/shared_preferences_linux.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';
import 'package:shared_preferences_web/shared_preferences_web.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';

/// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
Expand All @@ -24,20 +26,28 @@ class NativeSharedPreferences {
static Completer<NativeSharedPreferences>? _completer;
static bool _manualDartRegistrationNeeded = true;

static NativeSharedPreferencesStorePlatform get _store {
static SharedPreferencesStorePlatform get _store {
// This is to manually endorse the Linux implementation until automatic
// registration of dart plugins is implemented. For details see
// https://github.com/flutter/flutter/issues/52267.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
if (!kIsWeb &&
if (kIsWeb) {
return SharedPreferencesStorePlatform.instance =
SharedPreferencesPlugin();
} else if (!kIsWeb &&
NativeSharedPreferencesStorePlatform.instance
is MethodChannelSharedPreferencesStore) {
if (Platform.isLinux) {
SharedPreferencesStorePlatform.instance = SharedPreferencesLinux();
return SharedPreferencesStorePlatform.instance =
SharedPreferencesLinux();
} else if (Platform.isWindows) {
SharedPreferencesStorePlatform.instance = SharedPreferencesWindows();
return SharedPreferencesStorePlatform.instance =
SharedPreferencesWindows();
} else if (Platform.isMacOS) {
return SharedPreferencesStorePlatform.instance =
SharedPreferencesMacOS();
}
}
_manualDartRegistrationNeeded = false;
Expand Down Expand Up @@ -195,9 +205,15 @@ class NativeSharedPreferences {
}

Future<Map<String, Object>> getAllFromDictionary(List<String> keys) async {
final Map<String, Object> fromDictionary =
await _store.getAllFromDictionary(keys);
return fromDictionary;
if (_store is NativeSharedPreferencesStorePlatform) {
final Map<String, Object> fromDictionary =
await (_store as NativeSharedPreferencesStorePlatform)
.getAllFromDictionary(keys);
return fromDictionary;
} else {
return (await _store.getAll())
..removeWhere((key, value) => !key.contains(key));
}
}

/// Initializes the shared preferences with mock values for testing.
Expand Down
21 changes: 3 additions & 18 deletions lib/native_shared_preferences_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';

import 'method_channel_native_shared_preferences.dart';

Expand All @@ -15,7 +16,8 @@ import 'method_channel_native_shared_preferences.dart';
/// (using `extends`) ensures that the subclass will get the default implementation, while
/// platform implementations that `implements` this interface will be broken by newly added
/// [NativeSharedPreferencesStorePlatform] methods.
abstract class NativeSharedPreferencesStorePlatform {
abstract class NativeSharedPreferencesStorePlatform
extends SharedPreferencesStorePlatform {
/// The default instance of [NativeSharedPreferencesStorePlatform] to use.
///
/// Defaults to [MethodChannelNativeSharedPreferencesStore].
Expand Down Expand Up @@ -49,23 +51,6 @@ abstract class NativeSharedPreferencesStorePlatform {
/// Removes the value associated with the [key].
Future<bool> remove(String key);

/// Stores the [value] associated with the [key].
///
/// The [valueType] must match the type of [value] as follows:
///
/// * Value type "Bool" must be passed if the value is of type `bool`.
/// * Value type "Double" must be passed if the value is of type `double`.
/// * Value type "Int" must be passed if the value is of type `int`.
/// * Value type "String" must be passed if the value is of type `String`.
/// * Value type "StringList" must be passed if the value is of type `List<String>`.
Future<bool> setValue(String valueType, String key, Object value);

/// Removes all keys and values in the store.
Future<bool> clear();

/// Returns all key/value pairs persisted in this store.
Future<Map<String, Object>> getAll();

/// Returns all key/value pairs persisted in this store.
Future<Map<String, Object>> getAllFromDictionary(List<String> keys);

Expand Down
27 changes: 17 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,7 +21,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -92,7 +92,14 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: "direct main"
description:
Expand Down Expand Up @@ -155,7 +162,7 @@ packages:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
version: "2.0.13"
shared_preferences_android:
dependency: transitive
description:
Expand All @@ -176,14 +183,14 @@ packages:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.1.0"
shared_preferences_macos:
dependency: "direct main"
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
shared_preferences_platform_interface:
dependency: "direct main"
description:
Expand All @@ -197,14 +204,14 @@ packages:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
shared_preferences_windows:
dependency: "direct main"
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.1.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -251,7 +258,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
version: "0.4.8"
typed_data:
dependency: transitive
description:
Expand All @@ -265,7 +272,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
win32:
dependency: transitive
description:
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ dependencies:
sdk: flutter

meta: ^1.7.0
shared_preferences: ^2.0.9
shared_preferences: ^2.0.13
shared_preferences_platform_interface: ^2.0.0
# The design on https://flutter.dev/go/federated-plugins was to leave
# this constraint as "any". We cannot do it right now as it fails pub publish
# validation, so we set a ^ constraint.
# TODO(franciscojma): Revisit this (either update this part in the design or the pub tool).
# https://github.com/flutter/flutter/issues/46264
shared_preferences_linux: ^2.0.3
shared_preferences_macos: ^2.0.2
shared_preferences_web: ^2.0.2
shared_preferences_windows: ^2.0.3
shared_preferences_linux: ^2.1.0
shared_preferences_macos: ^2.0.3
shared_preferences_web: ^2.0.3
shared_preferences_windows: ^2.1.0

dev_dependencies:
flutter_test:
Expand Down