I'm in need to write an extension for Flutter's State<T extends StatefulWidget>
so I can use a function in all of my States, let's say showSnackBar("Hello world", 5)
.
I tried writing a mixin
mixin BaseState on State<ProfileScreen> {
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
void showSnackBar(String text) {
setState(() {
scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Row(
children: <Widget>[
new CircularProgressIndicator(),
new Text(text == null ? " Logging in" : " $text")
],
)));
});
}
void hideSnackBar() {
setState(() {
scaffoldKey.currentState.hideCurrentSnackBar();
});
}
}
As you can see, it is now mixed on State<ProfileScreen>
. It's a problem because I only can use this mixin in class ProfileScreenState extends State<ProfileScreen>
. Without the type notation I end up with an error:
error: The class 'ProfileScreenState' cannot implement both 'State<ProfileScreen>' and 'State<StatefulWidget>' 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)
I tried to Google a lot, seen questions like these but without a success.
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.