operator == method

  1. @override
bool operator ==(
  1. Object other
)
override

The StoreConnector has a distinct parameter which may be set to true. As a performance optimization, distinct:true allows the widget to be rebuilt only when the ViewModel changes. If this is not done, then every time any state in the store changes the widget will be rebuilt.

And then, of course, you must implement equals and hashcode for the ViewModel. This can be done by typing ALT+INSERT in IntelliJ IDEA or Android Studio and choosing ==() and hashcode, but you can't forget to update this whenever new parameters are added to the model. The present events must also be part of that equals/hashcode, like so:

  1. If the new ViewModel has an event which is not spent, then the ViewModel MUST be considered distinct, no matter the state of the old ViewModel, since the new event should fire.

  2. If both the old and new ViewModels have events which are spent, then these events MUST NOT be considered distinct, since spent events are considered "empty" and should never fire.

  3. If the new ViewModel has an event which is not spent, and the old ViewModel has an event which is spent, then the new event should fire, and for that reason they MUST be considered distinct.

  4. If the new ViewModel has an event which is is spent, and the old ViewModel has an event which not spent, then the new event should NOT fire, and for that reason they SHOULD NOT be considered distinct.

Note: To differentiate 3 and 4 we would actually be breaking the equals contract (which says A==B should be the same as B==A). Besides, we would need to know if AsyncRedux is comparing newVm==oldViewModel or oldViewModel==newVm (and stays like this). A safer alternative is discard 4, and always consider events different if any of them is not spent. That will, however, fire some unnecessary rebuilds.

In the near future, we may decide to break the equals contract (which is probably fine since the usage of Event is so specialized), and create unit tests to check it continues to work and detect breaks if new versions of AsyncRedux change the order of the comparison.

Implementation

@override
bool operator ==(Object other) {
  return identical(this, other) ||
      other is Event &&
          runtimeType == other.runtimeType

          /// 1) Events not spent are never considered equal to any other,
          /// and they will always "fire", forcing the widget to rebuild.
          /// 2) Spent events are considered "empty", so they are all equal.
          &&
          (isSpent && other.isSpent);
}