Advertisement
Beginner Flutter, State Management with Provider

What is ChangeNotifier, and how does calling notifyListeners let your interface automatically rebuild itself the moment a given piece of shared state actually genuinely changes?

Short answer

ChangeNotifier is a genuinely simple built in Flutter class that provides a straightforward mechanism for notifying any registered listeners whenever something genuinely changes, and a class extending ChangeNotifier simply calls its own inherited notifyListeners method after…

Save Flutter mock interview
Advertisement
AModel answer
ChangeNotifier is a genuinely simple built in Flutter class that provides a straightforward mechanism for notifying any registered listeners whenever something genuinely changes, and a class extending ChangeNotifier simply calls its own inherited notifyListeners method after properly updating its own internal state, which then automatically and properly triggers a rebuild of any widget that is genuinely currently listening to that particular class through Provider, making it a genuinely simple, quite lightweight foundation for building your own reactive state management classes.
class CartModel extends ChangeNotifier {
  final List<String> _items = [];

  void addItem(String item) {
    _items.add(item);
    notifyListeners();
  }
}
Real-world example
A CartModel class properly calls notifyListeners immediately after adding a genuinely new item to its own internal list, automatically causing every single widget currently displaying that cart's own contents to properly rebuild and correctly reflect that exact same newly added item.
Likely follow-ups
  • What genuinely happens if you forget to actually call notifyListeners after updating a given ChangeNotifier's own internal state?
  • How does ChangeNotifier genuinely relate to Flutter's own core underlying InheritedWidget mechanism?
Related topics