-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[shared_preferences] Platform interface for new shared preferences async #6962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+340
−11
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
packages/shared_preferences/shared_preferences_platform_interface/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...erences/shared_preferences_platform_interface/lib/in_memory_shared_preferences_async.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // 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 'shared_preferences_async_platform_interface.dart'; | ||
| import 'types.dart'; | ||
|
|
||
| /// Stores data in memory. | ||
| /// | ||
| /// Data does not persist across application restarts. This is useful in unit tests. | ||
| base class InMemorySharedPreferencesAsync | ||
| extends SharedPreferencesAsyncPlatform { | ||
| /// Instantiates an empty in-memory preferences store. | ||
| InMemorySharedPreferencesAsync.empty() : _data = <String, Object>{}; | ||
|
|
||
| /// Instantiates an in-memory preferences store containing a copy of [data]. | ||
| InMemorySharedPreferencesAsync.withData(Map<String, Object> data) | ||
| : _data = Map<String, Object>.from(data); | ||
|
|
||
| final Map<String, Object> _data; | ||
|
|
||
| @override | ||
| Future<bool> clear( | ||
| ClearPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| final PreferencesFilters filter = parameters.filter; | ||
| if (filter.allowList != null) { | ||
| _data.removeWhere((String key, _) => filter.allowList!.contains(key)); | ||
| } else { | ||
| _data.clear(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
| Future<Map<String, Object>> getPreferences( | ||
| GetPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| final PreferencesFilters filter = parameters.filter; | ||
| final Map<String, Object> preferences = Map<String, Object>.from(_data); | ||
| preferences.removeWhere((String key, _) => | ||
| filter.allowList != null && !filter.allowList!.contains(key)); | ||
| return preferences; | ||
| } | ||
|
|
||
| Future<bool> _setValue( | ||
| String key, | ||
| Object value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| _data[key] = value; | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setString( | ||
| String key, | ||
| String value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _setValue(key, value, options); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setInt( | ||
| String key, | ||
| int value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _setValue(key, value, options); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setDouble( | ||
| String key, | ||
| double value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _setValue(key, value, options); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setBool( | ||
| String key, | ||
| bool value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _setValue(key, value, options); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setStringList( | ||
| String key, | ||
| List<String> value, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _setValue(key, value, options); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getString( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _data[key] as String?; | ||
| } | ||
|
|
||
| @override | ||
| Future<bool?> getBool( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _data[key] as bool?; | ||
| } | ||
|
|
||
| @override | ||
| Future<double?> getDouble( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _data[key] as double?; | ||
| } | ||
|
|
||
| @override | ||
| Future<int?> getInt( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| return _data[key] as int?; | ||
| } | ||
|
|
||
| @override | ||
| Future<List<String>?> getStringList( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| final List<Object?>? data = _data[key] as List<Object?>?; | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return data?.cast<String>(); | ||
| } | ||
|
|
||
| @override | ||
| Future<Set<String>> getKeys( | ||
| GetPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ) async { | ||
| final Set<String> keys = _data.keys.toSet(); | ||
| if (parameters.filter.allowList != null) { | ||
| keys.retainWhere( | ||
| (String element) => parameters.filter.allowList!.contains(element)); | ||
| } | ||
|
|
||
| return keys; | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
...hared_preferences_platform_interface/lib/shared_preferences_async_platform_interface.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // 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 'types.dart'; | ||
|
|
||
| /// The interface that implementations of shared_preferences_async must implement. | ||
| abstract base class SharedPreferencesAsyncPlatform { | ||
| /// Constructs a SharedPreferencesAsyncPlatform. | ||
| SharedPreferencesAsyncPlatform(); | ||
|
|
||
| /// The instance of [SharedPreferencesAsyncPlatform] to use. | ||
| static SharedPreferencesAsyncPlatform? instance; | ||
|
|
||
| /// Stores the String [value] associated with the [key]. | ||
| Future<void> setString( | ||
| String key, | ||
| String value, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Stores the bool [value] associated with the [key]. | ||
| Future<void> setBool( | ||
| String key, | ||
| bool value, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Stores the double [value] associated with the [key]. | ||
| Future<void> setDouble( | ||
| String key, | ||
| double value, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Stores the int [value] associated with the [key]. | ||
| Future<void> setInt( | ||
| String key, | ||
| int value, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Stores the List<String> [value] associated with the [key]. | ||
| Future<void> setStringList( | ||
| String key, | ||
| List<String> value, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Retrieves the String [value] associated with the [key], if any. | ||
| /// | ||
| /// Throws a [TypeError] if the returned type is not a String. | ||
| Future<String?> getString( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Retrieves the bool [value] associated with the [key], if any. | ||
| /// | ||
| /// Throws a [TypeError] if the returned type is not a bool. | ||
| Future<bool?> getBool( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Retrieves the double [value] associated with the [key], if any. | ||
| /// | ||
| /// Throws a [TypeError] if the returned type is not a double. | ||
| Future<double?> getDouble( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Retrieves the int [value] associated with the [key], if any. | ||
| /// | ||
| /// Throws a [TypeError] if the returned type is not an int. | ||
| Future<int?> getInt( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Retrieves the List<String> [value] associated with the [key], if any. | ||
| /// | ||
| /// Throws a [TypeError] if the returned type is not a List<String>. | ||
| Future<List<String>?> getStringList( | ||
| String key, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Removes all keys and values in the store that match the given [parameters]. | ||
| Future<void> clear( | ||
| ClearPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Returns all key/value pairs persisting in this store that match the given [parameters]. | ||
| Future<Map<String, Object>> getPreferences( | ||
| GetPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
|
|
||
| /// Returns all keys persisting in this store that match the given [parameters]. | ||
| Future<Set<String>> getKeys( | ||
| GetPreferencesParameters parameters, | ||
| SharedPreferencesOptions options, | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.