Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Add @immutable annotation to Equatable class #25
Is your feature request related to a problem? Please describe.
At the moment Equatable only work for immutable classes, but you can extends Equatable in a classes that not are immutable
Describe the solution you'd like
@immutable class Equatable ....
Describe alternatives you've considered
does the class being extended from equatable really need to be fully immutable, or is it only the fields that are used in super([props]) as the comparators that need to be final? in the second case, the @immutable annotation causes a warning that is not be needed. if indeed all fields in the class must be immutable then the annotation is right. (I have a class with a few properties but only a couple are needed to verify equality)
this is kind of confusing but I hope this explanation is clear.
I am using flutter_bloc and the bloc has a list of objects (that extend equatable) in it. those objects are fetched one-by-one when the bloc is initialized. but one field in each object can't be set until all the objects are fetched because it depends on the content of the other objects. Its just an int and it isn't needed in the equality comparison. it has to do with what is in the JSON i receive and I don't have control of that.
So so right now I fetch the objects one by one, construct them, add them to the list, then make a pass through the list to update that one field. that prevents me from making that one field final. so i get the 'not immutable' warning.
I can refactor my code to use a temporary list that captures the data and updates the field. then creates the final list of immutable objects. this would get rid of the warning. but as you say if its technically ok to leave that field mutable then I will just leave it as is and ignore the warning.
Hello, @felangel thanks for the reply. Here's my simple example:
I have the
For that bloc I have this test:
I'm using the mocked
I'm pretty new to Flutter tests, maybe I shouldn't do things like this. What do you think?