Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Access Stored Sharedpreference value from Other Pages

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?

like image 289
Rajesh Jr. Avatar asked Jul 06 '18 17:07

Rajesh Jr.


People also ask

How can I get shared preference data in flutter?

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.

Where is shared preference file stored flutter?

SharedPreferences are stored in an xml file in the app data folder, i.e.

What are shared preferences in flutter?

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?

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.

What is the use of sharedpreferences?

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.

How to use sharedpreferences with storageutil?

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.

How do you store values in a shared preference?

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 ();


1 Answers

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.).

like image 64
Richard Heap Avatar answered Nov 02 '22 23:11

Richard Heap