Skip to content

[shared_preferences] Handle Date type objects on iOS and macOS. #4230

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

Closed
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
5 changes: 5 additions & 0 deletions packages/go_router/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences: {path: ../../shared_preferences/shared_preferences}}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences: {path: ../../../shared_preferences/shared_preferences}}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences: {path: ../../../shared_preferences/shared_preferences}}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences: {path: ../../../shared_preferences/shared_preferences}}
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.3

* Added handling of Date type objects on iOS in UserDefaults.

## 2.1.2

* Fixes singleton initialization race condition introduced during NNBD
Expand Down
2 changes: 2 additions & 0 deletions packages/shared_preferences/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ If the prefix is set to a value such as `''` that causes it to read values that
not originally stored by the `SharedPreferences`, initializing `SharedPreferences`
may fail if any of the values are of types that are not supported by `SharedPreferences`.

On iOS/macOS if UserDefaults include Date type objects, they are converted into timestamps.

If you decide to remove the prefix entirely, you can still access previously created
preferences by manually adding the previous prefix `flutter.` to the beginning of
the preference key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences: {path: ../../../shared_preferences/shared_preferences}, shared_preferences_foundation: {path: ../../../shared_preferences/shared_preferences_foundation}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.1.2
version: 2.1.3

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down Expand Up @@ -42,3 +42,8 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences_foundation: {path: ../../shared_preferences/shared_preferences_foundation}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* Updates minimum supported macOS version to 10.14.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 2.2.3

* Added handling of Date type objects on iOS in UserDefaults.

## 2.2.2

* Updates minimum iOS version to 11.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
import Foundation

#if os(iOS)
import Flutter
import Flutter
#elseif os(macOS)
import FlutterMacOS
import FlutterMacOS
#endif

public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi {
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = SharedPreferencesPlugin()
// Workaround for https://github.com/flutter/flutter/issues/118103.
#if os(iOS)
let messenger = registrar.messenger()
#else
let messenger = registrar.messenger
#endif
#if os(iOS)
let messenger = registrar.messenger()
#else
let messenger = registrar.messenger
#endif
UserDefaultsApiSetup.setUp(binaryMessenger: messenger, api: instance)
}

func getAllWithPrefix(prefix: String) -> [String? : Any?] {
return getAllPrefs(prefix: prefix)
func getAllWithPrefix(prefix: String) -> [String?: Any?] {
let prefs = getAllPrefs(prefix: prefix)
return convertDates(prefs)
}

func setBool(key: String, value: Bool) {
Expand Down Expand Up @@ -61,4 +62,27 @@ public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi {
}
return filteredPrefs
}

/// Recursively convert all Date type objects into timestamp.
private func convertDates<T>(_ value: T) -> T {
var result: Any
if value is Date {
result = Int((value as! Date).timeIntervalSince1970)
} else if value is [String: Any] {
var dict: [String: Any] = [:]
for (k, v) in value as! [String: Any] {
dict[k] = convertDates(v) as Any
}
result = dict
} else if value is [Any] {
var list: [Any] = []
for v in value as! [Any] {
list.append(convertDates(v))
}
result = list
} else {
result = value
}
return result as! T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ dev_dependencies:

flutter:
uses-material-design: true

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{shared_preferences_foundation: {path: ../../../shared_preferences/shared_preferences_foundation}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: shared_preferences_foundation
description: iOS and macOS implementation of the shared_preferences plugin.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_foundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.2.2
version: 2.2.3

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