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
Redux 对于带嵌套AppState的颤振减速器,最佳做法是什么_Redux_Dart_Flutter - Fatal编程技术网

Redux 对于带嵌套AppState的颤振减速器,最佳做法是什么

Redux 对于带嵌套AppState的颤振减速器,最佳做法是什么,redux,dart,flutter,Redux,Dart,Flutter,如果AppState包含列表或其他复杂对象,我想知道AppState reducer的最佳实践 举个例子: 设置:比如说,我在写一个体重跟踪应用程序,每个体重输入都有一个日期和评论。我想显示关于用户权重条目的各种信息,所以我决定将它们提升到appstate。我决定将它们从用户数据中分离出来,并将它们保存在一个单独的列表中,以使appstate尽可能平坦,并分离不同的方面(用户数据、设置、权重条目) 代码: AppState: class AppState{ //username, birth

如果AppState包含列表或其他复杂对象,我想知道AppState reducer的最佳实践

举个例子:

设置:比如说,我在写一个体重跟踪应用程序,每个体重输入都有一个日期和评论。我想显示关于用户权重条目的各种信息,所以我决定将它们提升到appstate。我决定将它们从用户数据中分离出来,并将它们保存在一个单独的列表中,以使appstate尽可能平坦,并分离不同的方面(用户数据、设置、权重条目)

代码:

AppState:

class AppState{
  //username, birthdate, etc.
  List<Entry> entries;
}
修改/添加条目的2个操作:

class UpdateCommentOnEntry{
  int index;
  String comment;
}

class AddEntry{
  Entry entry;
}
任务:在AppState Reducer wrt中处理此列表的最佳方法是什么。这两个动作

关注事项: 移动设备上的性能和内存管理。基本上,我希望尽可能少地复制

可能的解决办法:

-1) 据我所知,此解决方案是错误的/违反了Redux标准,因为它改变了当前状态:

AppState reducer(AppState state, dynamic action) {
  if(action is UpdateCommentOnEntry){
    state.entries[action.index].comment=action.comment;
  } else if(action is AddEntry){
    state.entries.add(action.entry);
  }
  return state;
}
1) 此解决方案复制整个列表

AppState reducer(AppState state, dynamic action) {
  if(action is UpdateCommentOnEntry){
    List<Entry> newList;
    for(int i = 0; i<state.entries.length; i++){
      if(i!=action.index){  
        newList.add(state.entries[i]);
      } else{
        newList.add(state.entries[i].copyWith(comment: action.comment));
      }
    } 
    return state.copyWith(entries = newList);
  } else if(action is AddEntry){
    List<Entry> newList = List<Entry>.from(state.entries);
    newList.entries.add(action.entry);
    return state.copyWith(entries: newList);
  } else {
    return state;
  }
}
AppState减速机(AppState状态,动态动作){
if(操作为updateCommentEntry){
列表新建列表;
对于(int i=0;i
AppState reducer(AppState state, dynamic action) {
  if(action is UpdateCommentOnEntry){
    List<Entry> newList;
    for(int i = 0; i<state.entries.length; i++){
      if(i!=action.index){  
        newList.add(state.entries[i]);
      } else{
        newList.add(state.entries[i].copyWith(comment: action.comment));
      }
    } 
    return state.copyWith(entries = newList);
  } else if(action is AddEntry){
    List<Entry> newList = List<Entry>.from(state.entries);
    newList.entries.add(action.entry);
    return state.copyWith(entries: newList);
  } else {
    return state;
  }
}
AppState reducer(AppState state, dynamic action) {
  if(action is UpdateCommentOnEntry){
    state.entries[i] = state.entries[i].copyWith(comment: action.comment);
    return state.copyWith(entries: state.entries);
  } else if(action is AddEntry){
    List<Entry> newList = List<Entry>.from(state.entries);
    newList.entries.add(action.entry);
    return state.copyWith(entries: newList);
  } else {
    return state;
  }
}