ChangeNotifierProxy Provider & ChangeNotifier Provider loses value when app is refreshed

Issue

I am using a ChangeNotifierProxyProvider to provide a class called DatabaseService. The provider is shown below

 ChangeNotifierProxyProvider<AuthService, DatabaseService>(
    create:(_)=> DatabaseService() ,
    update: (_,AuthService authService, DatabaseService databaseService)=> databaseService..update(authService.currentUserId),
    ), 

Here is the update function in DatabaseService

void update (String uid)async {

if(uid==this.uid) return;
this.uid=uid;
notifyListeners();
print("USER ID IS : " + uid); }

When a user logs in, the Database class successfully gets the uid, and calling Provider.of(context).uid works fine. The problem is that when I refresh the app, the uid resets back to its default value. How can I prevent the value from resetting after refreshing the app.

Solution

So I came to a conclusion that provider does not persist data. Meaning that when the app is killed or refreshed in the emulator, the provider values will reset. To solve my problem, I used share preferences to solve my data persistency problem.

Answered By – Damon Mohan

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.