Bloc: Change _bindStateSubject's check for currentState == nextState to use hashCode instead

Created on 25 Feb 2019  路  3Comments  路  Source: felangel/bloc

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
To trigger BlocBuilder for a UI rebuild, a new instance of State() needs to be returned in mapEventToState

(State nextState) {
        if (currentState == nextState) return; <-----------------
        final transition = Transition(
          currentState: _stateSubject.value, 
          event: currentEvent,
          nextState: nextState,
        );
        BlocSupervisor().delegate?.onTransition(transition);
        onTransition(transition);
        _stateSubject.add(nextState);
      },

So for example i have state looking like this

class SomeLoadedState extends SomeState {
  List<Card> all;
  List<Card> filtered;
  Map<int, int> hourMap;
  bool hideChart;
}

To switch the paramater hideChart I need to create a new Instance of SomeLoadedState and pass
all of the paramteres of the currentState to the new instance.

SomeLoadedState(
all:currentState.all,
filtered:currentState.filtered,
hourMap:currentState.hourMap,
hideChart:!currentState.hideChart
)

Describe the solution you'd like
A clear and concise description of what you want to happen.

I suggest to check not if the currentState == nextState , but
currentHash == nextState.hashCode


  int _currentHash; <--------------------

  void _bindStateSubject() {
    Event currentEvent;

    transform(_eventSubject).asyncExpand((Event event) {
      currentEvent = event;
      return mapEventToState(
        _stateSubject.value,
        event,
      ).handleError((Object error, StackTrace stacktrace) {
        onError(error, stacktrace);
        BlocSupervisor().delegate?.onError(error, stacktrace);
      });
    }).forEach(
      (State nextState) {
        if (_currentHash == nextState.hashCode) return;<--------------------
        _currentHash = nextState.hashCode;<--------------------
        final transition = Transition(
          currentState: _stateSubject.value,
          event: currentEvent,
          nextState: nextState,
        );
        BlocSupervisor().delegate?.onTransition(transition);
        onTransition(transition);
        _stateSubject.add(nextState);
      },
    );
  }
question

Most helpful comment

Hi, thanks for suggestion @felangel I will edit my code to use copyWith, that's one great optimization that will make my mapEventToState more readable.
Now that I'm having a second look at my suggestion I think, that doing with copyWith is stricter and would not lead to cases, where reading the code does not correctly represent the behavior you are expecting.
Also, thanks for the great work with bloc pattern implementation.

All 3 comments

Hi @tigranhov can you please provide a bit more information about why using hashCode would be better? I鈥檓 not sure I understand the problem you鈥檙e trying to solve. In the example you gave you鈥檇 still have the same problem because hashCodes should be the same for objects that are equal and should not change after an object has been instantiated (source).

I would suggest implementing copyWith so that you can create a new instance like state.copyWith(hideChart: true). Check this out for an example of copyWith.

Thanks!

Hi, thanks for suggestion @felangel I will edit my code to use copyWith, that's one great optimization that will make my mapEventToState more readable.
Now that I'm having a second look at my suggestion I think, that doing with copyWith is stricter and would not lead to cases, where reading the code does not correctly represent the behavior you are expecting.
Also, thanks for the great work with bloc pattern implementation.

Glad I could help and thanks for the positive feedback! 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nerder picture nerder  路  3Comments

rsnider19 picture rsnider19  路  3Comments

frankrod picture frankrod  路  3Comments

wheel1992 picture wheel1992  路  3Comments

krusek picture krusek  路  3Comments