title: How to write a mixin for a generic class State<T extends StatefulWidget>
body: <p>I'm in need to write an extension for Flutter's <code>State&lt;T extends StatefulWidget&gt;</code> so I can use a function in all of my States, let's say <code>showSnackBar("Hello world", 5)</code>.
  I tried writing a mixin </p>
  
  <pre><code>mixin BaseState on State&lt;ProfileScreen&gt; {
    final GlobalKey&lt;ScaffoldState&gt; scaffoldKey = new GlobalKey&lt;ScaffoldState&gt;();
  
    void showSnackBar(String text) {
      setState(() {
        scaffoldKey.currentState.showSnackBar(new SnackBar(
            content: new Row(
              children: &lt;Widget&gt;[
                new CircularProgressIndicator(),
                new Text(text == null ? "  Logging in" : "      $text")
              ],
            )));
      });
    }
  
    void hideSnackBar() {
      setState(() {
        scaffoldKey.currentState.hideCurrentSnackBar();
      });
    }
  }
  </code></pre>
  
  <p>As you can see, it is now mixed on <code>State&lt;ProfileScreen&gt;</code>. It's a problem because I only can use this mixin in <code>class ProfileScreenState extends State&lt;ProfileScreen&gt;</code>. Without the type notation I end up with an error:</p>
  
  <pre><code>error: The class 'ProfileScreenState' cannot implement both 'State&lt;ProfileScreen&gt;' and 'State&lt;StatefulWidget&gt;' because the type arguments are different. (conflicting_generic_interfaces at [mobile] lib/Screens/profile.dart:17)
  error: Type parameters could not be inferred for the mixin 'BaseState' because no type parameter substitution could be found matching the mixin's supertype constraints (mixin_inference_no_possible_substitution at [mobile] lib/Screens/profile.dart:17)
  </code></pre>
  
  <p>I tried to Google a lot, seen questions like <a href="https://stackoverflow.com/q/51476234/1044146">these</a> but without a success.</p>
  
  <p>And yes I know composition is preferred over inheritance in Flutter, but I think this is a thing I don't know I would make work with composition and I feel it will be OK with inheritance.</p>