Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Flutter SharedPreferences in Swift

Is it possible to access SharedPreferences saved from Flutter accessed in Swift code of plugin? In Android we have FILE mode for SharedPreferences. Any similar feature in Swift 4?

like image 742
sandy Avatar asked Dec 11 '22 02:12

sandy


2 Answers

The shared_preferences uses NSUserDefaults on iOS to store the data. You can easily access it with Swift like this:

let name = NSUserDefaults.standard.string(forKey: "flutter.test")
print(name)

It would also make sense to use the optional binding to get the value safely:

if let name = NSUserDefaults.standard.string(forKey: "flutter.test") {
    print(name)
}

Note, that if you use the key test in your flutter/dart code you would need to add the flutter. prefix to the key, as the shared_preferences plugin prefixes every key with it (see this line in the source code)

like image 141
ChrisG Avatar answered Jan 12 '23 17:01

ChrisG


Use UserDefaults on Swift.

UserDefaults.standard.object(forKey:"flutter.key"))

key = key used em flutter to shared preferences. You need to use flutter prefix on key.

like image 24
Xande Rasta Moura Avatar answered Jan 12 '23 16:01

Xande Rasta Moura