0

I have a state like this

class NameState{
    List name;
    dynamic title;
    NameState({this.title, this.name});
}

Action

class NameAction{
   List  showAction;
   NameAction(this.showAction);

}

reducer like this

NameState reducer(NameState state, dynamic action) {

 if (action is NameAction) {
  return NameState(
    name: []..addAll(action.showAction)
  );
}

 return state;

}

and initial state is defined like this initialState: new NameState(title: 'this is title', name: []), middleware: [thunkMiddleware]);

Here is the main problem,

you can see I have given a static string to title variable. It works fine and visible in all pages.

But as soon as reducer is called this title gets a value of null. I don't want to pass any value to title. It should be as it is with it's own value. I was updating only the name list.

In javascript state management, we can update any variable we want without affecting other variables in the state. How can I do it in flutter redux?

Thank you.

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95

1 Answers1

1

You need to pass all the variables of the current state when returning a new state.

NameState reducer(NameState state, dynamic action) {
  if (action is NameAction) {
    return NameState(
      name: []..addAll(action.showAction),
      title: state.title,
    );
  }

  return state;
}
Michael Yuwono
  • 2,452
  • 1
  • 14
  • 32
  • it gives this error when using rebuild ```The method 'rebuild' isn't defined for the class 'NameState'. Try correcting the name to the name of an existing method, or defining a method named 'rebuild'.``` – Hkm Sadek May 29 '19 at 17:27
  • Oops! Sorry, I didn't realise that you did not use build runner implementation. I've updated my solution. – Michael Yuwono May 29 '19 at 22:49