How To Remove SharedPreferences Item From An Isolate In Flutter

Issue

I am using flutter_isolate plugin and I am trying to remove an item from SharedPreferences in an isolate. But it doesn’t seem to work. The item still exists on the main thread when i try to access it.
Below is my code to remove the item in the isolate:

SharedPreferences pref = await SharedPreferences.getInstance();
bool x = await pref.remove('image_post_data');
if (x == true) print('removed image_post_data'); // This is printed out

But when I try to access image_post_data on the main thread, I still get the value instead of getting a null. What am I missing ?

Solution

Reload Your SharedPreferences instance after updating it in your isolate and before using it in your main thread. By calling

pref.reload()

Answered By – Taha Malik

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.