Member-only story

How To Use ChangeNotifier

Greg Perry
6 min readNov 22, 2024

It can’t be used in certain cases. Not directly anyway.

Have you ever tried to use the ChangeNotifier as a Mixin on a State class? You can’t do it.

class NotifierState extends State<NotifierWidget> with ChangeNotifier;

If you try, your State class will no longer call its dispose() method, and you need to call its dispose() method to ‘clean’ things up in many cases. However, the ChangeNotifier is a Mixin with its own dispose() method, and it doesn’t extend a parent class with a dispose() function. Therefore, as you can see in the second screenshot below, its dispose() method doesn’t have to call, super.dispose(). It just won’t work as a Mixin on any class that has its own dispose() method. However, there is a way to introduce a ChangeNotifier with a Mixin by doing it indirectly.

Other Stories by Greg Perry

Supply the Mixin,ImplNotifyListenersChangeNotifierMixin, to your State class, and place the method call,disposeChangeNotifier(), to its dispose() method. You‘ll then have a ChangeNotifier working with your State class.

  @override
@mustCallSuper
void dispose() {
// Call the dispose of the implementation of Change Notifier…

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Greg Perry
Greg Perry

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations