Hkm Sadek
Hkm Sadek

Reputation: 3209

Flutter redux update the specific state variable without affecting other variable

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.

Upvotes: 0

Views: 1141

Answers (1)

Michael Yuwono
Michael Yuwono

Reputation: 2617

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;
}

Upvotes: 1

Related Questions