Flutter: Access Stored Sharedpreference value from Other Pages

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 … Read more

SharedPreferences value is not updated

Instead of using edit.commit();, you should use edit.apply();. Apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values. commit() Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is … Read more

How to save and retrieve Date in SharedPreferences

To save and load accurate date, you could use the long (number) representation of a Date object. Example: //getting the current time in milliseconds, and creating a Date object from it: Date date = new Date(System.currentTimeMillis()); //or simply new Date(); //converting it back to a milliseconds representation: long millis = date.getTime(); You can use this … Read more

How to detect if changes were made in the preferences?

Do SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // your stuff here } }; In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method. That’s what I do and I never have a problem. Else it’s your conditional … Read more

How to save List to SharedPreferences?

It only possible to use primitive types because preference keep in memory. But what you can use is serialize your types with Gson into json and put string into preferences: private static SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_FILE_NAME, Context.MODE_PRIVATE); private static SharedPreferences.Editor editor = sharedPreferences.edit(); public <T> void setList(String key, List<T> list) { Gson gson = new … Read more

Android – SharedPreferences with serializable object

The accepted answer is misleading, we can store serializable object into SharedPreferences by using GSON. Read more about it at google-gson. you can add GSON dependency in Gradle file with: compile ‘com.google.code.gson:gson:2.7’ Here the snippet: First, create your usual sharedPreferences: //Creating a shared preference SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); Saving from serializable object to preference: Editor … Read more

Preference items being automatically re-set?

I was able to reproduce this issue. I also found a workaround but I don’t know why it works 🙂 Create a derived class from SwitchPreference like so: public class Pref extends SwitchPreference { public Pref(Context context) { super(context); } public Pref(Context context, AttributeSet attrs) { super(context, attrs); } public Pref(Context context, AttributeSet attrs, int … Read more

SharedPreferences in BroadcastReceiver seems to not update?

I had the same problem and after struggling for hours to solve it, I finally found the issue causing it. In your AndroidManifest you probably have something like that: <receiver android:name=”AlarmReceiver” android:process=”:remote” /> The last attribute (process:remote) cause the receiver to run on a different/new process when it is called. But SharedPreferences is NOT supported … Read more

Writing Singleton Class To Manage Android SharedPreferences

Proper Singleton Shared Preferences Class. it may help others in the future. public class SharedPref { private static SharedPreferences mSharedPref; public static final String NAME = “NAME”; public static final String AGE = “AGE”; public static final String IS_SELECT = “IS_SELECT”; private SharedPref() { } public static void init(Context context) { if(mSharedPref == null) mSharedPref … Read more