I'm storing a value in a page called page1.dart
. I want to access the stored value from page2.dart
or page3.dart
. How can I achieve this?
To use the shared_preferences plugin, we have to import it into our file: import 'package:shared_preferences/shared_preferences. dart'; The shared_preferences plugin exports a SharedPreferences class, which has methods that can be used to set data of various primitive types in SharedPreferences.
SharedPreferences are stored in an xml file in the app data folder, i.e.
shared_preferences is a Flutter plugin that allows you to save data in a key-value format so you can easily retrieve it later. Behind the scenes, it uses the aptly named SharedPreferences on Android and the similar UserDefaults on iOS.
How to Save and Read Data from Shared Preferences in Flutter is a simple use case of shared preferences. We will develop a simple app in which you can increase or decrease the number shown in the text filed with the help of these two buttons. The integer value in the. text field is coming from the saved preferences.
SharedPreferences is what Android and iOS apps use to store simple data in an allocated space. This data exists even when the app is shut down and starts up again; we can still retrieve the value as it was. The data stored in SharedPreferences can be edited and deleted. SharedPreferences stores the data in a key-value pair.
class StorageUtil { static StorageUtil _storageUtil; static SharedPreferences _preferences; } Actually Shared Preference is using async and await method. so it will use Future to put and get from Shared preference. Make getInstance method to get Storage and set this method to init so it will init every time.
Store Values in Shared Preferences You can store values in shared preferences in the form of string, integer, boolean, double and list of strings. setStringValue (String key, String value) async { SharedPreferences myPrefs = await SharedPreferences. getInstance ();
Flutter shared preferences is actually implemented as an in-memory cache. The first time that you call SharedPreferences.getInstance()
all the current values are read from NSUserDefaults (on iOS) and SharedPreferences (on Android) and cached in memory. This involves channels, so is async. The Future returns a singleton class that wraps this cache. Any subsequent calls to getInstance return this singleton class.
When you get a value from shared preferences the value is simply fetched from the in-memory cache. When you set a value the cache is immediately updated and an async method started to write it back to the operating system. (You can wait for confirmation of completion of that, but you don't have to.) Note that the reads and writes from and to the cache are synchronous, so you have immediate access to that cache of settings. (It may take a moment to reach the operating system so the in-memory cache isn't guaranteed to match the device preferences, but this should only happen under error conditions.)
The reason why this is interesting is that once you get the result of SharedPreferences.getInstance()
you can read and write values synchronously. Since the instance is singleton it seems reasonable to keep a copy of it. In particular, you can make your main async and fetch it there.
SharedPreferences sp;
void main() async {
sp = await SharedPreferences.getInstance();
runApp(new MyApp());
}
This allows you to refer to sp
throughout your code:
onPressed: () {
sp.setString('abc', 'def');
},
knowing that the in-memory cache is consistent. In particular, any values set in one page can be got in another page following navigation.
Having said all that, you should probably think of your stored preferences as just one part of your state, which just happens to get initialized at startup and on setting it fires off a background task to persist it automatically. You could then deal with the preferences in the same way as the rest of your state however you are doing that (InheritedWidget + controller, Redux, Streams, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With