Heng YouSour
Heng YouSour

Reputation: 153

reset field or variable in Provider

i'm using provider package for state management , anyone can show me the right way reset field or variable ? example code here : i want to reset _counter back to 0 after i navigate to other screen , i need the right way to do it , thank you in advance :)

class AppProvider with ChangeNotifier {
int _counter = 0;
int get counter => _counter;

void incrementCounter() {
_counter++;
notifyListeners();
}
}

enter image description here

Upvotes: 0

Views: 675

Answers (1)

Mohammad_Asef
Mohammad_Asef

Reputation: 336

add this method and call it when you navigate to other page:

class AppProvider with ChangeNotifier {
int _counter = 0;
int get counter => _counter;

 void incrementCounter() {
  _counter++;
  notifyListeners();
 }
 void clear(){
  _counter = 0;
  notifyListeners();
 }
}

Upvotes: 2

Related Questions