Bloc: Does it ever make sense for events to be Equatable?

Created on 16 Jul 2020  路  2Comments  路  Source: felangel/bloc

Not really a bug, I think, I would just really appreciate your input on this.

I understand the reason states need to override the operator== method or use Equatable, but I fail to see the reason why it should be the case for events. When I call the same event multiple times, no equality comparison or filtering happens by default, as it does for states. It is best visible in the counter tutorial: https://bloclibrary.dev/#/fluttercountertutorial?id=counter-events - the event is an enum (so firing e.g. increment multiple times actually uses the same instance, which is always equal to itself), end firing the same event multiple times keeps incrementing or decrementing the value, depending on the event. This is a proof that equality doesn't apply to events by default, right?

Our codebase (one that you know very, very well, at a renowned car producer ;d) is full of events that extend Equatable. Also, pretty much all event types in your tutorials are also Equatable. Why is that? When does it apply? I think I can safely drop extends Equatable from my events and it will work all the same.

question

All 2 comments

Hi @wujek-srujek 馃憢
Thanks for opening an issue!

In many cases, whether or not events are Equatable makes no difference; however, it does matter when you are overriding transformEvents (for example when applying distinct) and also makes it easier to write tests in many cases because you can do things like:

// If MyEvent is not Equatable, this will not work
verify(myBloc.add(MyEvent(value: 'some-value'))).called(1);

Hope that helps answer your question 馃憤

Hi. Yes, I know transformEvents may use it, that's why I wrote 'by default not comparison / filtering is applied'. Good point about the tests, but we don't write them like this (at least in my feature module).
That clears a lot of things up, thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

komapeb picture komapeb  路  3Comments

wheel1992 picture wheel1992  路  3Comments

Reidond picture Reidond  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

nerder picture nerder  路  3Comments