skip to Main Content

Sorry my bad English in advance.

I would like to find out how I can clear SharedPreferences when I log out of the application. In my case, the Logout button is on screen 1 (HomePage).

On screen 2, I’m keeping items in a list that generates a Listview with several tiles. When I leave screen 2 to go to another screen, the list items must be kept so that the listview is displayed.This part kinda works.

However, when I log out of the application and log into another account, items from the account I was previously logged into appear. I am aware of the existence of .clear() but I don’t know how it would be implemented given that the list is on one screen and the logout button on another.

  1. I’ve tried to use clear on the HomePage screen but it didn’t work.
  2. Tried to create an abstract class involving SharedPreferences but turns out I am not that skillful at managing classes.

2

Answers


  1. I think using Shared Prefs to store a data item that’s common between several users is a bad choice. How would you distinguish them (It’s your problem here)

    such common data should be stored in a SQL DB or Baas like firebase.

    Shared prefs is generally used to store fast accessible elements like the theme mode or even whether the user is a new user or not.

    Login or Signup to reply.
  2. considering you are just storing id of the user from api and clearing it after logging out.

    use shared preference

    set data

    // Obtain shared preferences.
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    
    // Save an String value to 'action' key.
    await prefs.setString('userName', 'abcd1');
    

    delete data

    await prefs.remove('userName');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search