-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[shared_preferences] Add shared preferences devtools #6749
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
Changes from 1 commit
9a98dce
f6a1ce9
b9c0b6b
e2f1478
b798091
0115bea
421d981
bf23f70
2402e78
215d551
18ac82b
16f30fc
c14409e
382f612
aba50c5
a118924
0604550
eb6dc64
c9dab29
1627ec8
c7c3eb5
bb58727
ee1d2f4
01d7f5c
39b3990
00e568c
21202e6
e489086
c32457f
c6c8068
c4be218
862938b
84c6103
866ef50
7b1a151
bffbe95
d8af525
26f186b
8b005d9
2dc2a33
5b7cbbf
c497026
bc887de
ffd64a9
e95d285
ed56a14
8936da6
966d0fc
3267c1b
1acebd4
3846f73
6eb1b4f
d3fea9e
af0dfca
107fc82
4b2f619
6abec8a
a45bc1c
c1a9c85
612e118
ea80fce
1ef04f6
4df0393
2f8e7ea
b77893c
7a3ec34
e9b913c
8362269
119df62
38954ba
e3c39f2
9f7cdd4
026b225
659b9cd
7bd20c1
d672329
e6da5b8
3c5cecd
c761404
2b7d31f
c80795a
bb0b9be
4f6e93b
4f2d2fd
9838731
a783959
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // 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:convert'; | ||
| import 'dart:developer' as developer; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
|
|
||
| import '../shared_preferences.dart'; | ||
|
|
||
| const String _eventPrefix = 'shared_preferences:'; | ||
|
|
||
| /// A typedef for the post event function | ||
|
||
| @visibleForTesting | ||
| typedef PostEvent = void Function( | ||
| String eventKind, | ||
| Map<String, Object?> eventData, | ||
| ); | ||
|
|
||
| /// A helper class that provides data to the devtool extension. | ||
|
||
| /// | ||
| /// It is only visible for testing and eval. | ||
| @visibleForTesting | ||
| class DevtoolsExtension { | ||
|
||
| /// The default constructor for [DevtoolsExtension]. | ||
| /// | ||
| /// Accepts an optional [PostEvent] that should only be overwritten when testing. | ||
| DevtoolsExtension([this._postEvent = developer.postEvent]); | ||
|
||
|
|
||
| final PostEvent _postEvent; | ||
|
|
||
| /// Requests all legacy and async keys and post an event with the result. | ||
| Future<void> requestAllKeys() async { | ||
| final SharedPreferences legacyPrefs = await SharedPreferences.getInstance(); | ||
| final Set<String> legacyKeys = legacyPrefs.getKeys(); | ||
| final Set<String> asyncKeys = await SharedPreferencesAsync().getKeys(); | ||
|
|
||
| _postEvent('${_eventPrefix}all_keys', <String, List<String>>{ | ||
| 'asyncKeys': asyncKeys.toList(), | ||
| 'legacyKeys': legacyKeys.toList(), | ||
| }); | ||
| } | ||
|
|
||
| /// Requests the value for a given key and post and event with the result. | ||
|
||
| Future<void> requestValue(String key, bool legacy) async { | ||
| final Object? value; | ||
| if (legacy) { | ||
| final SharedPreferences legacyPrefs = | ||
| await SharedPreferences.getInstance(); | ||
| value = legacyPrefs.get(key); | ||
| } else { | ||
| value = await SharedPreferencesAsync().getAll(allowList: <String>{ | ||
| key | ||
| }).then((Map<String, Object?> map) => map.values.firstOrNull); | ||
| } | ||
|
|
||
| _postEvent('${_eventPrefix}value', <String, Object?>{ | ||
| 'value': value, | ||
| // It is safe to use `runtimeType` here. This code | ||
| // will only ever run in debug mode. | ||
| 'kind': value.runtimeType.toString(), | ||
| }); | ||
| } | ||
|
|
||
| /// Requests the a value change for the give key and post an empty event when finished; | ||
|
||
| Future<void> requestValueChange( | ||
| String key, | ||
| String serializedValue, | ||
| String kind, | ||
| bool legacy, | ||
| ) async { | ||
| final Object? value = jsonDecode(serializedValue); | ||
| if (legacy) { | ||
| final SharedPreferences legacyPrefs = | ||
| await SharedPreferences.getInstance(); | ||
| // we need to check the kind because sometimes a double | ||
| // gets interpreted as an int. If this was not and issue | ||
|
||
| // we'd only need to do a simple pattern matching on value. | ||
| switch (kind) { | ||
| case 'int': | ||
| await legacyPrefs.setInt(key, value! as int); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in the way it is implemented. There is no way to set it as null. The only option is removing it, but this is another method. |
||
| case 'bool': | ||
| await legacyPrefs.setBool(key, value! as bool); | ||
| case 'double': | ||
| await legacyPrefs.setDouble(key, value! as double); | ||
| case 'String': | ||
| await legacyPrefs.setString(key, value! as String); | ||
| case 'List<String>': | ||
| await legacyPrefs.setStringList( | ||
| key, | ||
| (value! as List<Object?>).cast(), | ||
| ); | ||
| } | ||
| } else { | ||
| final SharedPreferencesAsync prefs = SharedPreferencesAsync(); | ||
| // we need to check the kind because sometimes a double | ||
| // gets interpreted as an int. If this was not and issue | ||
|
||
| // we'd only need to do a simple pattern matching on value. | ||
| switch (kind) { | ||
| case 'int': | ||
| await prefs.setInt(key, value! as int); | ||
| case 'bool': | ||
| await prefs.setBool(key, value! as bool); | ||
| case 'double': | ||
| await prefs.setDouble(key, value! as double); | ||
| case 'String': | ||
| await prefs.setString(key, value! as String); | ||
| case 'List<String>': | ||
| await prefs.setStringList( | ||
| key, | ||
| (value! as List<Object?>).cast(), | ||
| ); | ||
| } | ||
| } | ||
| _postEvent('${_eventPrefix}change_value', <String, Object?>{}); | ||
| } | ||
|
|
||
| /// Requests a key removal and post an empty event when removed. | ||
|
||
| Future<void> requestRemoveKey(String key, bool legacy) async { | ||
| if (legacy) { | ||
| final SharedPreferences legacyPrefs = | ||
| await SharedPreferences.getInstance(); | ||
| await legacyPrefs.remove(key); | ||
| } else { | ||
| await SharedPreferencesAsync().remove(key); | ||
| } | ||
| _postEvent('${_eventPrefix}remove', <String, Object?>{}); | ||
| } | ||
| } | ||
|
|
||
| /// Include a variable to keep the library alive in web builds. | ||
| /// It must be a `final` variable. | ||
| /// Check this discussion for more info: https://github.com/flutter/packages/pull/6749/files/6eb1b4fdce1eba107294770d581713658ff971e9#discussion_r1755375409 | ||
| // ignore: prefer_const_declarations | ||
| final bool fieldToKeepDevtoolsExtensionLibraryAlive = false; | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this be
shared_preferences.instead ofshared_preferences:? Using dot is a more common convention for event names than using a colon.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I copied this from somewhere, gonna change it to
..