18

After upgrading to Flutter 3.10 (and Dart 3), I am getting this error:

The class 'PreferredSizeWidget' can't be used as a mixin because it's neither a mixin class nor a mixin.

The current version of the code:

class MyAppBar extends StatelessWidget with PreferredSizeWidget {
  ...
}
Hossein Yousefpour
  • 3,827
  • 3
  • 23
  • 35

1 Answers1

60

If you get the same error, just change the "with" with the "implements" like this:

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  ...
}

Description:

In Dart 3, the rules around mixins have been made stricter compared to previous versions. As mentioned in the release notes, any class could be used as a mixin before Dart 3, as long as it had no declared constructors and no superclass other than Object. In Dart 3, classes declared in libraries at language version 3.0 or later can't be used as mixins unless they are explicitly marked with the mixin keyword.

So, PreferredSizeWidget is not declared as a mixin and does not have the mixin keyword in its definition. This is why the error is encountered when using it as a mixin.

Hossein Yousefpour
  • 3,827
  • 3
  • 23
  • 35
  • Is that not a typo to replace simply `with` by `implements` ? It's like just like using wrong key word? – Elikill58 Jun 03 '23 at 12:55
  • No, it’s not a typo. It’s a syntax change required by the new rules of mixins in Dart 3. You can’t use any class as a mixin unless it is explicitly marked with the mixin keyword. PreferredSizeWidget is not marked as a mixin, so you can’t use it with the “with” keyword. You have to use the “implements” keyword instead, meaning you have to provide your implementation of the methods defined by PreferredSizeWidget. – Hossein Yousefpour Jun 29 '23 at 15:16