Skip to content
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

fix: SharedPreferencesAdapter ~ type 'List<Object?>' is not a subtype of type 'FutureOr<List<String>?>' in type cast #49

Merged
merged 1 commit into from
Jun 1, 2022
Merged
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
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>9.0</string>
</dict>
</plist>
14 changes: 7 additions & 7 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
PODS:
- Flutter (1.0.0)
- shared_preferences (0.0.1):
- shared_preferences_ios (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
shared_preferences:
:path: ".symlinks/plugins/shared_preferences/ios"
shared_preferences_ios:
:path: ".symlinks/plugins/shared_preferences_ios/ios"

SPEC CHECKSUMS:
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.10.1
COCOAPODS: 1.11.2
4 changes: 2 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -156,7 +156,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
</dict>
</plist>
54 changes: 26 additions & 28 deletions lib/src/impl/shared_preferences_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {
static Future<T> _wrapFutureOr<T>(FutureOr<T> value) =>
value is Future<T> ? value : SynchronousFuture<T>(value);

/// Create [SharedPreferencesAdapter] from [SharedPreferences].
static FutureOr<SharedPreferencesAdapter> from(
FutureOr<SharedPreferences> prefsOrFuture,
) =>
prefsOrFuture is Future<SharedPreferences>
? prefsOrFuture.then((p) => SharedPreferencesAdapter._(p))
: SharedPreferencesAdapter._(prefsOrFuture)
as FutureOr<SharedPreferencesAdapter>;

@override
Future<void> clear([void _]) =>
_prefs.clear().throwsIfNotSuccess('Cannot clear');
Expand All @@ -27,42 +36,20 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {
_wrap(_prefs.containsKey(key));

@override
Future<Map<String, Object?>> reload() {
return _prefs.reload().then((_) {
return {
for (final k in _prefs.getKeys()) k: _prefs.get(k),
};
});
}
Future<Map<String, Object?>> reload() =>
_prefs.reload().then((_) => _getAllFromPrefs());

@override
Future<void> remove(String key, [void _]) =>
_prefs.remove(key).throwsIfNotSuccess('Cannot remove key=$key');

/// Create [SharedPreferencesAdapter] from [SharedPreferences].
static FutureOr<SharedPreferencesAdapter> from(
FutureOr<SharedPreferences> prefsOrFuture,
) =>
prefsOrFuture is Future<SharedPreferences>
? prefsOrFuture.then((p) => SharedPreferencesAdapter._(p))
: SharedPreferencesAdapter._(prefsOrFuture)
as FutureOr<SharedPreferencesAdapter>;

@override
Future<T?> read<T extends Object>(String key, Decoder<T?> decoder, [void _]) {
var val = _prefs.get(key);
if (val is List) {
val = _prefs.getStringList(key);
}
return _wrapFutureOr(decoder(val));
}
Future<T?> read<T extends Object>(String key, Decoder<T?> decoder,
[void _]) =>
_wrapFutureOr(decoder(_getFromPrefs(key)));

@override
Future<Map<String, Object?>> readAll([void _]) {
return _wrap({
for (final k in _prefs.getKeys()) k: _prefs.get(k),
});
}
Future<Map<String, Object?>> readAll([void _]) => _wrap(_getAllFromPrefs());

@override
Future<void> write<T extends Object>(
Expand All @@ -74,6 +61,17 @@ class SharedPreferencesAdapter implements SharedPreferencesLike {
: _write(encodedOrFuture, key, value);
}

Object? _getFromPrefs(String key) {
final val = _prefs.get(key);
return val is List ? _prefs.getStringList(key) : val;
}

Map<String, Object?> _getAllFromPrefs() {
return {
for (final k in _prefs.getKeys()) k: _getFromPrefs(k),
};
}

Future<void> _write(Object? encoded, String key, Object? value) {
assert(encoded is! Future<dynamic>,
'The actual type of encoded value is ${encoded.runtimeType}');
Expand Down