0

When I try to attach a mixin to a flutter state, I want to overide the build function of the state like what we do in widget inheritence, however, every time when I tried to do so. it gives me an error.

Is there any one know how to override a state's build function in Mixins? Or is it actually not allowed? If it is not allowed, then why?

Thank you very much!

  • 1
    `class FooWithMixin extends StatefulWidget { @override _FooWithMixinState createState() => _FooWithMixinState(); } class _FooWithMixinState extends State with FooMixin { Widget build(BuildContext context) { return Column( children: [ super.build(context), Text('from state'), ], ); } } class FooMixin { Widget build(BuildContext context) { return Text('from mixin'); } }` – pskink Oct 22 '21 at 06:54
  • sure, your welcome, but actually why dont you want to extend `State` class and use it as a base class for other `State` classes? – pskink Oct 22 '21 at 07:29
  • I am sorry, I didn't actually fully understand what you mean here, do you mean extend the state with the foo function and use that state for the stateful widget? – Jack duan Oct 22 '21 at 07:39
  • AH yeah, I get your idea, the reason why I don't want to use that is probably because of the collaboration, I want the other developers to simply call a with a do all the job rather than go through inheritence. – Jack duan Oct 22 '21 at 07:43
  • actually man, what i am confused here is why mixin is called in the super function, why it is not the abstract class call the build function as super in mixin. Thank you – Jack duan Oct 22 '21 at 07:45
  • sorry i dont know what you mean – pskink Oct 22 '21 at 08:00

0 Answers0