Sitemap

Member-only story

How To Use ChangeNotifier

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.

Press enter or click to view image in full size
Press enter or click to view image in full size
change_notifier.dart and change_notifier.dart

Table of Contents

· Change the Mix
· The Field of Change
· Add Some Class
· Build Change
· Listen For Change

Press enter or click to view image in full size
Other Stories by Greg Perry

Change the Mix

--

--