0

I have a mixin (MyMixin) that I apply in the state of some widgets like this:

    class MyStfulWidget extends StatefulWidget {
      @override
      _MyStfulWidget State createState() => _MyStfulWidget State();
    }
    
    class _MyStfulWidget State extends State<MyStfulWidget> with MyMixin { ... }

I sometimes need to pass this stateful widgets as a parameter for a method (MyMethod)

    void myMethod(Widget myWidget) { ... }

The problem with the following method is that it accepts every kind of widgets. If I pass a widget that does not have a MyMixin it will not work properly...

Is there any way to force the "myWidget" parameter to be a stateful widget that contains a mixin in its state?

Thanks for the help!

Tiago Santos
  • 726
  • 6
  • 13

1 Answers1

1

First of all, you said you passed StatefulWidget to MyWidget method but MyMixin is on its State and State is private so this is impossible in this case. In context of your MyMixin you should mix it on StatefulWidget itself and change the prototype of method to

void myMethod<T extends MyMixin>(T widget);
BambinoUA
  • 6,126
  • 5
  • 35
  • 51