Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Flutter Flatter redux:StoreConnector与StoreProvider_Flutter_Redux_Flutter Redux - Fatal编程技术网

Flutter Flatter redux:StoreConnector与StoreProvider

Flutter Flatter redux:StoreConnector与StoreProvider,flutter,redux,flutter-redux,Flutter,Redux,Flutter Redux,我使用Flatter_redux的时间只有几天,我想知道它们之间的区别是什么: class BtnCustom extends StatelessWidget { @override Widget build(BuildContext context) { final store = StoreProvider.of<AppState>(context); return FlatButton( onPressed: store.dispatch(

我使用Flatter_redux的时间只有几天,我想知道它们之间的区别是什么:

class BtnCustom extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final store = StoreProvider.of<AppState>(context);

    return FlatButton(
      onPressed: store.dispatch(MyCustomAction),
      child: Text(store.state.MyCustomTxt),
    );
  }
}
类BtnCustom扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
最终存储=StoreProvider.of(上下文);
返回按钮(
onPressed:store.dispatch(MyCustomAction),
子项:文本(store.state.MyCustomTxt),
);
}
}

类BtnCustom扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回存储连接器(
转换器:(存储)=>\u视图模型(
txt:store.state.MyCustomTxt,
onPressed:store.dispatch(MyCustomAction)),
生成器:(BuildContext上下文,_viewmodelvm){
返回按钮(
onPressed:vm.onPressed,
子项:文本(vm.txt),
);
},
);
}
}
类_视图模型{
最终字符串txt;
最后的void函数()按下;
_ViewModel({this.txt,this.onPressed});
}
?

第一个似乎很好用。我应该注意的是,使用一种方法比另一种方法有什么优点或缺点吗

根据文档,StoreConnector将在其中重建小部件,以便:

  • 当您不需要重建小部件时,不使用StoreConnector可以吗
  • 在StoreConnector中有多个小部件可以吗

StoreConnector
为您提供了对小部件的更多控制,尤其是当您不想重建它时<代码>存储连接器:

  • 如果使用
    distinct:true
    并在
    ViewModel
    中覆盖
    hashCode
    ==
    ,则可以检测
    ViewModel
    是否已更改(是否应重建小部件)

  • 通过快速检查某些特定的
    存储区,可以完全跳过小部件重建。状态

     StoreConnector<AppState, MyViewModel>(
         distinct: true,
         ignoreChange: (state) {      
             return state.someVariable == theValueIDontCareAbout;
         },
         ...
     ),
    
     class MyViewModel{
         @override
         bool operator ==(other) {
              return (other is MyViewModel) && (this.someVmVariable == other.someVmVariable);
         }
    
         @override
         int get hashCode {
             int result = 17;
             result = 37 * result + someVmVariable.hashCode;
             return result;
         }
     }
    
    StoreConnector(
    是的,
    ignoreChange:(州){
    return state.someVariable==值idontcareaout;
    },
    ...
    ),
    类MyViewModel{
    @凌驾
    布尔运算符==(其他){
    return(其他为MyViewModel)&&(this.someVmVariable==other.someVmVariable);
    }
    @凌驾
    int获取哈希代码{
    int结果=17;
    result=37*result+someVmVariable.hashCode;
    返回结果;
    }
    }
    
以及一些更细粒度的控件。请查看
StoreConnector
构造函数的文档。如果store sonnector中的多个小部件共享同一个ViewModel,那么自然会有这样的小部件。但是,如果可以通过分离它们的
ViewModel
s来防止它们重新折叠,则可以使用分离的
StoreConnector
s

 StoreConnector<AppState, MyViewModel>(
     distinct: true,
     ignoreChange: (state) {      
         return state.someVariable == theValueIDontCareAbout;
     },
     ...
 ),

 class MyViewModel{
     @override
     bool operator ==(other) {
          return (other is MyViewModel) && (this.someVmVariable == other.someVmVariable);
     }

     @override
     int get hashCode {
         int result = 17;
         result = 37 * result + someVmVariable.hashCode;
         return result;
     }
 }