Skip to content

Commit b04ff46

Browse files
authored
[shared_preferences] Platform interface for new shared preferences async (flutter#6962)
Platform interface portion of flutter/packages#5210 Adds new async api
1 parent 976dfce commit b04ff46

File tree

7 files changed

+340
-11
lines changed

7 files changed

+340
-11
lines changed

packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.4.0
22

3+
* Adds `SharedPreferencesAsyncPlatform` API.
34
* Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.
45

56
## 2.3.2

packages/shared_preferences/shared_preferences_platform_interface/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ same interface.
99
# Usage
1010

1111
To implement a new platform-specific implementation of `shared_preferences`, extend
12-
[`SharedPreferencesPlatform`][2] with an implementation that performs the
13-
platform-specific behavior, and when you register your plugin, set the default
14-
`SharedPreferencesLoader` by calling the `SharedPreferencesPlatform.loader` setter.
12+
[`SharedPreferencesPlatform`][2] and [`SharedPreferencesAsyncPlatform`][3] with
13+
implementations that perform the platform-specific behaviors, and when you register
14+
your plugin, set the default `SharedPreferencesStorePlatform` and
15+
`SharedPreferencesAsyncPlatform` by calling the `SharedPreferencesPlatform.instance`
16+
and `SharedPreferencesAsyncPlatform.instance` setters.
17+
18+
Please note that the plugin tooling only registers the native and/or Dart classes
19+
listed in your package's `pubspec.yaml`, so if you intend to implement more than
20+
one class, you will need to manually register the second class
21+
(as can be seen in the Android and iOS implementations).
1522

1623
# Note on breaking changes
1724

@@ -23,3 +30,4 @@ on why a less-clean interface is preferable to a breaking change.
2330

2431
[1]: ../shared_preferences
2532
[2]: lib/shared_preferences_platform_interface.dart
33+
[3]: lib/shared_preferences_async_platform_interface.dart
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'shared_preferences_async_platform_interface.dart';
8+
import 'types.dart';
9+
10+
/// Stores data in memory.
11+
///
12+
/// Data does not persist across application restarts. This is useful in unit tests.
13+
base class InMemorySharedPreferencesAsync
14+
extends SharedPreferencesAsyncPlatform {
15+
/// Instantiates an empty in-memory preferences store.
16+
InMemorySharedPreferencesAsync.empty() : _data = <String, Object>{};
17+
18+
/// Instantiates an in-memory preferences store containing a copy of [data].
19+
InMemorySharedPreferencesAsync.withData(Map<String, Object> data)
20+
: _data = Map<String, Object>.from(data);
21+
22+
final Map<String, Object> _data;
23+
24+
@override
25+
Future<bool> clear(
26+
ClearPreferencesParameters parameters,
27+
SharedPreferencesOptions options,
28+
) async {
29+
final PreferencesFilters filter = parameters.filter;
30+
if (filter.allowList != null) {
31+
_data.removeWhere((String key, _) => filter.allowList!.contains(key));
32+
} else {
33+
_data.clear();
34+
}
35+
return true;
36+
}
37+
38+
@override
39+
Future<Map<String, Object>> getPreferences(
40+
GetPreferencesParameters parameters,
41+
SharedPreferencesOptions options,
42+
) async {
43+
final PreferencesFilters filter = parameters.filter;
44+
final Map<String, Object> preferences = Map<String, Object>.from(_data);
45+
preferences.removeWhere((String key, _) =>
46+
filter.allowList != null && !filter.allowList!.contains(key));
47+
return preferences;
48+
}
49+
50+
Future<bool> _setValue(
51+
String key,
52+
Object value,
53+
SharedPreferencesOptions options,
54+
) async {
55+
_data[key] = value;
56+
return true;
57+
}
58+
59+
@override
60+
Future<bool> setString(
61+
String key,
62+
String value,
63+
SharedPreferencesOptions options,
64+
) async {
65+
return _setValue(key, value, options);
66+
}
67+
68+
@override
69+
Future<bool> setInt(
70+
String key,
71+
int value,
72+
SharedPreferencesOptions options,
73+
) async {
74+
return _setValue(key, value, options);
75+
}
76+
77+
@override
78+
Future<bool> setDouble(
79+
String key,
80+
double value,
81+
SharedPreferencesOptions options,
82+
) async {
83+
return _setValue(key, value, options);
84+
}
85+
86+
@override
87+
Future<bool> setBool(
88+
String key,
89+
bool value,
90+
SharedPreferencesOptions options,
91+
) async {
92+
return _setValue(key, value, options);
93+
}
94+
95+
@override
96+
Future<bool> setStringList(
97+
String key,
98+
List<String> value,
99+
SharedPreferencesOptions options,
100+
) async {
101+
return _setValue(key, value, options);
102+
}
103+
104+
@override
105+
Future<String?> getString(
106+
String key,
107+
SharedPreferencesOptions options,
108+
) async {
109+
return _data[key] as String?;
110+
}
111+
112+
@override
113+
Future<bool?> getBool(
114+
String key,
115+
SharedPreferencesOptions options,
116+
) async {
117+
return _data[key] as bool?;
118+
}
119+
120+
@override
121+
Future<double?> getDouble(
122+
String key,
123+
SharedPreferencesOptions options,
124+
) async {
125+
return _data[key] as double?;
126+
}
127+
128+
@override
129+
Future<int?> getInt(
130+
String key,
131+
SharedPreferencesOptions options,
132+
) async {
133+
return _data[key] as int?;
134+
}
135+
136+
@override
137+
Future<List<String>?> getStringList(
138+
String key,
139+
SharedPreferencesOptions options,
140+
) async {
141+
final List<Object>? data = _data[key] as List<Object>?;
142+
return data?.cast<String>();
143+
}
144+
145+
@override
146+
Future<Set<String>> getKeys(
147+
GetPreferencesParameters parameters,
148+
SharedPreferencesOptions options,
149+
) async {
150+
final Set<String> keys = _data.keys.toSet();
151+
if (parameters.filter.allowList != null) {
152+
keys.retainWhere(
153+
(String element) => parameters.filter.allowList!.contains(element));
154+
}
155+
156+
return keys;
157+
}
158+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'types.dart';
8+
9+
/// The interface that implementations of shared_preferences_async must implement.
10+
abstract base class SharedPreferencesAsyncPlatform {
11+
/// Constructs a SharedPreferencesAsyncPlatform.
12+
SharedPreferencesAsyncPlatform();
13+
14+
/// The instance of [SharedPreferencesAsyncPlatform] to use.
15+
static SharedPreferencesAsyncPlatform? instance;
16+
17+
/// Stores the String [value] associated with the [key].
18+
Future<void> setString(
19+
String key,
20+
String value,
21+
SharedPreferencesOptions options,
22+
);
23+
24+
/// Stores the bool [value] associated with the [key].
25+
Future<void> setBool(
26+
String key,
27+
bool value,
28+
SharedPreferencesOptions options,
29+
);
30+
31+
/// Stores the double [value] associated with the [key].
32+
Future<void> setDouble(
33+
String key,
34+
double value,
35+
SharedPreferencesOptions options,
36+
);
37+
38+
/// Stores the int [value] associated with the [key].
39+
Future<void> setInt(
40+
String key,
41+
int value,
42+
SharedPreferencesOptions options,
43+
);
44+
45+
/// Stores the List<String> [value] associated with the [key].
46+
Future<void> setStringList(
47+
String key,
48+
List<String> value,
49+
SharedPreferencesOptions options,
50+
);
51+
52+
/// Retrieves the String [value] associated with the [key], if any.
53+
///
54+
/// Throws a [TypeError] if the returned type is not a String.
55+
Future<String?> getString(
56+
String key,
57+
SharedPreferencesOptions options,
58+
);
59+
60+
/// Retrieves the bool [value] associated with the [key], if any.
61+
///
62+
/// Throws a [TypeError] if the returned type is not a bool.
63+
Future<bool?> getBool(
64+
String key,
65+
SharedPreferencesOptions options,
66+
);
67+
68+
/// Retrieves the double [value] associated with the [key], if any.
69+
///
70+
/// Throws a [TypeError] if the returned type is not a double.
71+
Future<double?> getDouble(
72+
String key,
73+
SharedPreferencesOptions options,
74+
);
75+
76+
/// Retrieves the int [value] associated with the [key], if any.
77+
///
78+
/// Throws a [TypeError] if the returned type is not an int.
79+
Future<int?> getInt(
80+
String key,
81+
SharedPreferencesOptions options,
82+
);
83+
84+
/// Retrieves the List<String> [value] associated with the [key], if any.
85+
///
86+
/// Throws a [TypeError] if the returned type is not a List<String>.
87+
Future<List<String>?> getStringList(
88+
String key,
89+
SharedPreferencesOptions options,
90+
);
91+
92+
/// Removes all keys and values in the store that match the given [parameters].
93+
Future<void> clear(
94+
ClearPreferencesParameters parameters,
95+
SharedPreferencesOptions options,
96+
);
97+
98+
/// Returns all key/value pairs persisting in this store that match the given [parameters].
99+
Future<Map<String, Object>> getPreferences(
100+
GetPreferencesParameters parameters,
101+
SharedPreferencesOptions options,
102+
);
103+
104+
/// Returns all keys persisting in this store that match the given [parameters].
105+
Future<Set<String>> getKeys(
106+
GetPreferencesParameters parameters,
107+
SharedPreferencesOptions options,
108+
);
109+
}

packages/shared_preferences/shared_preferences_platform_interface/lib/types.dart

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,62 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
/// Filter options used to get and clear preferences.
5+
// shared_preferences_async types.
6+
7+
import 'package:flutter/foundation.dart';
8+
9+
/// Basic options for creating SharedPreferencesAsync classes.
10+
///
11+
/// This class exists to provide extension to platform specific options as
12+
/// there are currently no general options that are not platform specific.
13+
@immutable
14+
class SharedPreferencesOptions {
15+
/// Constructor for SharedPreferencesOptions.
16+
const SharedPreferencesOptions();
17+
}
18+
19+
/// Filter options used to get and clear preferences on shared_preferences_async.
20+
@immutable
21+
class PreferencesFilters {
22+
/// Creates a new instance with the given options.
23+
const PreferencesFilters({
24+
this.allowList,
25+
});
26+
27+
/// A list of preference keys that will limit getting and clearing to only
28+
/// items included in this list.
29+
///
30+
/// An empty set will create a filter that allows no items to be set/get.
31+
final Set<String>? allowList;
32+
}
33+
34+
/// Parameters for use in [get] methods on shared_preferences_async.
35+
@immutable
36+
class GetPreferencesParameters {
37+
/// Creates a new instance with the given options.
38+
const GetPreferencesParameters({required this.filter});
39+
40+
/// Filter to limit which preferences are returned.
41+
final PreferencesFilters filter;
42+
}
43+
44+
/// Parameters for use in [clear] methods on shared_preferences_async.
45+
@immutable
46+
class ClearPreferencesParameters {
47+
/// Creates a new instance with the given options.
48+
const ClearPreferencesParameters({required this.filter});
49+
50+
/// Filter to limit which preferences are cleared.
51+
final PreferencesFilters filter;
52+
}
53+
54+
//////////////////////////////////
55+
// legacy_shared_preferences types.
56+
//////////////////////////////////
57+
58+
/// Filter options used to get and clear preferences on legacy_shared_preferences.
659
class PreferencesFilter {
7-
/// Constructor.
60+
/// Creates a new instance with the given options.
861
PreferencesFilter({
962
required this.prefix,
1063
this.allowList,
@@ -19,18 +72,18 @@ class PreferencesFilter {
1972
Set<String>? allowList;
2073
}
2174

22-
/// Parameters for use in [getAll] methods.
75+
/// Parameters for use in [getAll] methods on legacy_shared_preferences.
2376
class GetAllParameters {
24-
/// Constructor.
77+
/// Creates a new instance with the given options.
2578
GetAllParameters({required this.filter});
2679

2780
/// Filter to limit which preferences are returned.
2881
PreferencesFilter filter;
2982
}
3083

31-
/// Parameters for use in [clear] methods.
84+
/// Parameters for use in [clear] methods on legacy_shared_preferences.
3285
class ClearParameters {
33-
/// Constructor.
86+
/// Creates a new instance with the given options.
3487
ClearParameters({required this.filter});
3588

3689
/// Filter to limit which preferences are cleared.

packages/shared_preferences/shared_preferences_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences_platform_interface
22
description: A common platform interface for the shared_preferences plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_platform_interface
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
5-
version: 2.3.2
5+
version: 2.4.0
66

77
environment:
88
sdk: ^3.2.0

0 commit comments

Comments
 (0)