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

4,386

Solution 1

You can just do this:

prefrences.remove("keyName")

Dart code is pretty easy to read so sometimes when there isn't sufficient documentation, you may just dive into code: https://github.com/flutter/plugins/blob/481e8c251667bcb28d177bfc7d295d584e703bae/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart#L146

Solution 2

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

Share:
4,386
GaganSailor
Author by

GaganSailor

I am a software developer. I am interested in learning of new technologies.

Updated on December 18, 2022

Comments

  • GaganSailor
    GaganSailor over 1 year

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