[Flutter/Dart] The class can’t be used as a mixin because it extends a class other than Object. Error [mixin_inherits_from_not_object]
I was working on my Flutter Project yesterday, I encountered very strange error that i cannot use a class as mixin just because i didn’t extended it from Object Class (Object Class is the Mother of all the classes in Dart Language).
Then i searched and find this useful info:
This article provides a brief theoretical presentation of the evolution of mixins in Dart. Support for mixins changed in Dart 1.13, and it will again in Dart 2:
- Dart 1.12 or lower supports mixins that must extend
Object
, and must not callsuper()
. - Dart 1.13 or greater supports mixins that can extend from classes other than
Object
, and can callsuper.method()
. This support is only available by default in the Dart VM and in Analyzer behind a flag. More specifically, it is behind the--supermixin
flag in the command-line analyzer. It is also available in the analysis server, behind a client-configurable option. Dart2js and dartdevc do not support super mixins. - In Dart 2.1, mixins are expected to have fewer restrictions. For example, Flutter supports mixins calling
super()
and extending from a class other thanObject
, but the syntax is expected to change before appearing in all Dart SDKs. For details, see the mixin specification.
Now the problem was how to implement the solution in my code editor (as i am using vscode), That long section i didn’t find useful enough to solve this problem.
Let me help you,
Include this code in your “pubspec.yaml” file at the very end
Now the remaining problem was how to make understand my code editor that it is not an error anymore.
To Solve that Create a file named analysis_options.yaml
in the same directory as pubspec.yaml
and put the following into it.
Make Sure you indent the two files as shown in the image as .yaml files read information based on indentation.
For copying and pasting the code here it is (indent yourself using tab in vscode):
analyzer:
strong-mode: true
language:
enableSuperMixins: true
analyzer:
errors:
mixin_inherits_from_not_object: ignore
If you find this post helped in any way, please leave some claps for it and Maybe follow :) I will keep posting all the errors or experience i encounter during my coding journey!