Flutter shared_preferences not persistent?
Asked Answered
M

4

9

I have been using shared_preferences in flutter, was working fine until now. Suddenly it stopped working, both in iOS and Android. I debugged it step by step and it stores data to pref and while app is on, data still persists, but after hot restart _preferencecache always is empty. How can I solve this? (version is 0.5.12)

When user logs in I save the user_id:

final prefs = await SharedPreferences.getInstance();
 final userData = json.encode(
  {
    'user_id': userID,
  },
 );
prefs.setString('userData', userData);

Later, when user restarts again:

final prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('userData')) {
// print("no user data in shared preference");
  return false;
}

But the abpve function returns false, that's the issue, I checked the previous version of shared_preferences as well, but no solution.

Medication answered 12/10, 2020 at 14:27 Comment(4)
This plugin uses native shared preferences in the background (For android) and User Defaults (iOS). Haven't faced such issue yet. Which version of android are you running your app on? 10?Seedling
You might have to show us more code. I have been using SharedPreferences since the it's been available and have never had this issue, nor have I seen anyone report anything similar.Rebeckarebeka
The above code is the main part where I use sharedpreferences, After getting data, I write it into prefs and while app is running, I can easily see it there but later after a restart, no data is there. What should I show more, maybe you can specify, so I add..Medication
Yes 10, but I tried in 9 in emulator as well the same result unfortunately:(Medication
S
2

I assume that somewhere in your code, you faced this error and as a quick solution, you had added SharedPreferences.setMockInitialValues({}); in your code, which should be the reason (other than sharedPreferences.clear()).

The SharedPreferences.setMockInitialValues({}); is the thing that is preventing data to persist between sessions.

A quick getaway is to add a try-catch block to your code. Somethink like the following:

try {
      prefs.getInt(YOUR_KEY_HERE);
} catch (e) {
      SharedPreferences.setMockInitialValues({});
}

But this isn't a conventional fix to this problem, I recommend checking out this answer by Siddharth Agrawal.

Silverware answered 5/11, 2022 at 7:4 Comment(0)
H
2

you have do it like this

final prefs = await SharedPreferences.getInstance();

final data = prefs.getString("userData");

if(data != null){

      final userData = json.dncode(userData);
}

   
Hrutkay answered 5/1, 2023 at 6:33 Comment(0)
J
1

I realized I was clearing my shared preferences some where in my app and I had forgotten about it. Please check every where in your code for sharedPreferences.clear(). You never know.

Jurisconsult answered 11/5, 2022 at 10:59 Comment(0)
F
0

Check and remove SharedPreferences.setMockInitialValues({}); from your code. It will work after.

Foreignism answered 3/1 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.