Should I use Extends or Mixin with ChangeNotifier? | Flutter Tips

Should I use Extends or Mixin with ChangeNotifier?

Should I use Extends or Mixin with ChangeNotifier?

Extends vs Mixin in provider, what is really the difference between them?

You must have seen many tutorials extending ChangeNotifier by using “extend” or “with” keyword, and the code works using both, So why should you use mixin instead of extend, if you are creating Provider in my Flutter project, This is a new Flutter Tip with me.

What is Extends?


If you remember in the OOP, There is something called “inheritance”, And to inherit a child class from the parent class we use extend keyword after the class name, and when you do this you inherit everything to your new child class.

Let me explain what is inheritance means, if you have class Y extend class X, so every property and function exists in class X, it will be available to use in class Y.

So you can use extend to update an old class without building it from scratch, by inheriting from an old class and you can add more functions to it, or override the existing one.

What is Mixin?


Mixin is like a usage license, It is a way to use a class’s code but without inheriting the original class properties, you can say it’s like a “lite version of inheritance”, we use mixin in Flutter to quickly include code snippets that we use repeatedly, in order to reuse pieces of code without using inheritance.

Like in Provider you will always use mixin with ChangeNotifier to use a common function which is notifylisteners() function, and we use it to notify listeners that there is a change happened in data that we manage with the Provider.   


What is the difference between Extends and Mixin?

The first difference, “extends” is the normal OOP inheritance, and you can inherit a class with all its properties, variables, and functions and Override it if you want.

But “mixin” is permission to use other class functions instead of creating a new child class.

The second difference, You can’t extend more than one class, but you can mixin more than one class (and this is very important and this is the answer to the post question).

Using Extends vs Mixin in Provider


When you want to create Provider in Flutter, You will need to use notifylisteners() function, so we need to extend or mixin ChangeNotifier class to be able to do this, But if you extend that means ChangeNotifier will be the super class of the current provider and will not be able to implement any other super class with ChangeNotifier class, in normal situations that is ok, because we are not going to extend another class in our provider most of time.

Now, if your provider is already extended a super class, you can’t extend more than one class, and Dart won’t allow this unless you mixin your provider with the super class and ChangeNotifier class and more if you want, and this is why you need to mixin instead of extending in provider 

Can I mixin with already extended class?


Yes you can use both extend and mixin at the same time, If your provider is extended from another class, you can extend and mixin at the same time like this below class

       class item extends Product with ChangeNotifier {

      Your class




Congratulations, you’ve reached the end! I think you need a short summary with the key differences between “extends” and “with”.

- Use extends to inherit one class

- And use with to a mixin more than class.

In Provider, Both will allow you to use notifyListeners() method, But

- Extending means that ChangeNotifier is the super class, and 

- Mixin just allows you to use the methods from ChangeNotifier class 

You can read more about mixin:

Dart: What are mixins?

Dart for Flutter : Mixins in Dart

Don't like to Read?   Watch!!!  

⭐⭐⭐  Buy Food for my Dog 🐶 ⭐⭐⭐


#buttons=(Accept !) #days=(14)

Our website uses cookies to enhance your experience. Learn More
Accept !