'How to listen to variable changes in a provider class?

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


Solution 1:[1]

You can use addListener for instance of your class.

CoolProvider coolProvider = CoolProvider();

void f() {
 // access new data
}

coolProvider.addListener(f);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BeHappy