Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.3.0

* Adds `clearWithParameters` and `getAllWithParameters` methods.
* Deprecates `clearWithPrefix` and `getAllWithPrefix` methods.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 2.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:async';
import 'package:flutter/services.dart';

import 'shared_preferences_platform_interface.dart';
import 'types.dart';

const MethodChannel _kChannel =
MethodChannel('plugins.flutter.io/shared_preferences');
Expand Down Expand Up @@ -39,25 +40,58 @@ class MethodChannelSharedPreferencesStore
}

@override
@Deprecated('Use clearWithParameters instead')
Future<bool> clearWithPrefix(String prefix) async {
return clearWithParameters(
ClearParameters(
filter: PreferencesFilter(prefix: prefix),
),
);
}

@override
Future<bool> clearWithParameters(ClearParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
return (await _kChannel.invokeMethod<bool>(
'clearWithPrefix',
<String, dynamic>{'prefix': prefix},
'clearWithParameters',
<String, dynamic>{
'prefix': filter.prefix,
'allowList': filter.allowList?.toList(),
},
))!;
}

@override
Future<Map<String, Object>> getAllWithPrefix(String prefix) async {
return await _kChannel.invokeMapMethod<String, Object>(
'getAllWithPrefix',
<String, dynamic>{'prefix': prefix},
) ??
Future<Map<String, Object>> getAll() async {
return await _kChannel.invokeMapMethod<String, Object>('getAll') ??
<String, Object>{};
}

@override
Future<Map<String, Object>> getAll() async {
return await _kChannel.invokeMapMethod<String, Object>('getAll') ??
@Deprecated('Use getAllWithParameters instead')
Future<Map<String, Object>> getAllWithPrefix(
String prefix, {
Set<String>? allowList,
}) async {
return getAllWithParameters(
GetAllParameters(
filter: PreferencesFilter(prefix: prefix),
),
);
}

@override
Future<Map<String, Object>> getAllWithParameters(
GetAllParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
final List<String>? allowListAsList = filter.allowList?.toList();
return await _kChannel.invokeMapMethod<String, Object>(
'getAllWithParameters',
<String, dynamic>{
'prefix': filter.prefix,
'allowList': allowListAsList
},
) ??
<String, Object>{};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/foundation.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'method_channel_shared_preferences.dart';
import 'types.dart';

/// The interface that implementations of shared_preferences must implement.
///
Expand Down Expand Up @@ -69,11 +70,17 @@ abstract class SharedPreferencesStorePlatform extends PlatformInterface {
/// prefix 'flutter.'.
Future<bool> clear();

/// Removes all keys and values in the store with given prefix.
/// Removes all keys and values in the store with given [prefix].
@Deprecated('Use clearWithParameters instead')
Future<bool> clearWithPrefix(String prefix) {
throw UnimplementedError('clearWithPrefix is not implemented.');
}

/// Removes all keys and values in the store that match [options].
Future<bool> clearWithParameters(ClearParameters parameters) {
throw UnimplementedError('clearWithParameters is not implemented.');
}

/// Returns all key/value pairs persisted in this store where the key starts with 'flutter.'.
///
/// This default behavior is for backwards compatibility with older versions of this
Expand All @@ -82,9 +89,16 @@ abstract class SharedPreferencesStorePlatform extends PlatformInterface {
Future<Map<String, Object>> getAll();

/// Returns all key/value pairs persisting in this store that have given [prefix].
@Deprecated('Use getAllWithParameters instead')
Future<Map<String, Object>> getAllWithPrefix(String prefix) {
throw UnimplementedError('getAllWithPrefix is not implemented.');
}

/// Returns all key/value pairs persisting in this store that match [options].
Future<Map<String, Object>> getAllWithParameters(
GetAllParameters parameters) {
throw UnimplementedError('getAllWithParameters is not implemented.');
}
}

/// Stores data in memory.
Expand All @@ -103,24 +117,42 @@ class InMemorySharedPreferencesStore extends SharedPreferencesStorePlatform {

@override
Future<bool> clear() async {
return clearWithPrefix(_defaultPrefix);
return clearWithParameters(
ClearParameters(
filter: PreferencesFilter(prefix: _defaultPrefix),
),
);
}

@override
Future<bool> clearWithPrefix(String prefix) async {
_data.removeWhere((String key, _) => key.startsWith(prefix));
Future<bool> clearWithParameters(ClearParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
if (filter.allowList != null) {
_data.removeWhere((String key, _) =>
key.startsWith(filter.prefix) && filter.allowList!.contains(key));
} else {
_data.removeWhere((String key, _) => key.startsWith(filter.prefix));
}
return true;
}

@override
Future<Map<String, Object>> getAll() async {
return getAllWithPrefix(_defaultPrefix);
return getAllWithParameters(
GetAllParameters(
filter: PreferencesFilter(prefix: _defaultPrefix),
),
);
}

@override
Future<Map<String, Object>> getAllWithPrefix(String prefix) async {
Future<Map<String, Object>> getAllWithParameters(
GetAllParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
final Map<String, Object> preferences = Map<String, Object>.from(_data);
preferences.removeWhere((String key, _) => !key.startsWith(prefix));
preferences.removeWhere((String key, _) =>
!key.startsWith(filter.prefix) ||
(filter.allowList != null && !filter.allowList!.contains(key)));
return preferences;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.

/// Filter options used to get and clear preferences.
class PreferencesFilter {
/// Constructor.
PreferencesFilter({
required this.prefix,
this.allowList,
});

/// A prefix to limit getting and clearing to only items that begin with
/// this string.
String prefix;

/// A list of preference keys that will limit getting and clearing to only
/// items included in this list.
Set<String>? allowList;
}

/// Parameters for use in [getAll] methods.
class GetAllParameters {
/// Constructor.
GetAllParameters({required this.filter});

/// Filter to limit which preferences are returned.
PreferencesFilter filter;
}

/// Parameters for use in [clear] methods.
class ClearParameters {
/// Constructor.
ClearParameters({required this.filter});

/// Filter to limit which preferences are cleared.
PreferencesFilter filter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_platform_interface
description: A common platform interface for the shared_preferences plugin.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_platform_interface
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.2.0
version: 2.3.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Loading