How to delete/clear shared-preferences using key in flutter?

Viewed 6195

I am using the following code which clears all the shared-preferences but I just want to clear/delete particular shared-preferences value using key in a flutter.

For now, I have this which is clearing all the shared-preferences.

 SharedPreferences prefrences = await SharedPreferences.getInstance();
await prefrences.clear();
3 Answers

You can delete a particular key if you know in advance which key you want to delete

SharedPreferences sharedPreference = await SharedPreferences.getInstance();
    List<String> keys = sharedPreference.getKeys();
    keys.remove("username");

Hopes that helps

SharedPreferences prefrences = await SharedPreferences.getInstance(); await prefrences.remove("keyName");

Related