How To Use ChangeNotifier
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.
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…