Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 如何为泛型类状态编写mixin<;T扩展StatefulWidget>;_Inheritance_Dart_Flutter_Mixins - Fatal编程技术网

Inheritance 如何为泛型类状态编写mixin<;T扩展StatefulWidget>;

Inheritance 如何为泛型类状态编写mixin<;T扩展StatefulWidget>;,inheritance,dart,flutter,mixins,Inheritance,Dart,Flutter,Mixins,我需要为flatter的状态编写一个扩展,这样我就可以在我所有的状态下使用一个函数,比如说showSnackBar(“Hello world”,5)。 我试着写一个混音 mixin BaseState on State<ProfileScreen> { final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>(); void showSnackBar(Strin

我需要为flatter的
状态
编写一个扩展,这样我就可以在我所有的状态下使用一个函数,比如说
showSnackBar(“Hello world”,5)
。 我试着写一个混音

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();
    });
  }
}
mixin BaseState on State{
最终GlobalKey脚手架键=新的GlobalKey();
void showSnackBar(字符串文本){
设置状态(){
scaffoldKey.currentState.showSnackBar(新SnackBar(
内容:新行(
儿童:[
新的CircularProgressIndicator(),
新文本(Text==null?“登录”:“$Text”)
],
)));
});
}
void hideSnackBar(){
设置状态(){
scaffoldKey.currentState.hideCurrentSnackBar();
});
}
}
正如您所看到的,它现在在
状态下是混合的。这是一个问题,因为我只能在
类ProfileScreenState扩展状态中使用这个mixin。如果没有类型表示法,我最终会出现一个错误:

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)
错误:“ProfileScreenState”类无法同时实现“State”和“State”,因为类型参数不同。(在[mobile]lib/Screens/profile.dart:17处存在冲突的通用接口)
错误:无法为mixin“BaseState”推断类型参数,因为找不到与mixin的超类型约束匹配的类型参数替换(mixin\u推断\u可能的\u替换位于[mobile]lib/Screens/profile.dart:17)
我试着用谷歌搜索了很多问题,但都没有成功


是的,我知道在《颤栗》中,构图比继承更受欢迎,但我想这是一件我不知道我会如何处理构图的事情,我觉得继承也没问题。

我想你正在寻找类似的东西

mixin BaseState on State{
我做了一些类似的事情

混合素基态{ .... }


它正在工作,但我不知道后遗症。

我与状态{
上的混入BaseState非常接近。非常感谢!无论如何,万一我想,在某个时候,将它混入无状态小部件,有没有一个快速的技巧来接受它?
在无状态小部件上混入BaseStateless{
在T{
上混合BaseStateless应该是无状态的混合,如果我想在一个混合中共享StatefulWidget和无状态Widget的功能呢?这有意义吗?我的意思是类似于状态{
上的
混合BaseState,我想在这种情况下,你需要删除X上的
mixin BaseState<T extends StatefulWidget> on State<T> {