2

When using providers, how can I listen to a value change in the provider class? In the code below, when _myData changes, I want to change _allData as well. (I know I can just change them both together, but the code below is a stripped version of what I have)

class CoolProvider extends ChangeNotifier {
  List<String> _allData = [];
  List<String> _myData = [];

  List<OrdersResponse> get allData => _allData;
  List<OrdersResponse> get myData => _myData;

  void changeData()  {
      _allData = ['yo', 'bro'];
      notifyListeners();
  }
}
Jessica
  • 9,379
  • 14
  • 65
  • 136

1 Answers1

1

You can use addListener for instance of your class.

CoolProvider coolProvider = CoolProvider();

void f() {
 // access new data
}

coolProvider.addListener(f);
BeHappy
  • 3,705
  • 5
  • 18
  • 59