Hi, in the examples given in the bloc documentation. All the models are extended to Equatable. For example, in Todo example, the Todo extends the Equatable.
I want to know why to use Equatable in a model class?
When the class extends Equatable, it will make an object immutable. The way to change fields' value is to copy the previous object with the new value. Will this method consume more computer resources than just modifying a filed in a mutable object?
For example, if I have a class, that the fields need to updated frequently. And, one of its field might be big (e.g historyRecords). In this case, is it a good way to extends Equatable?
// without equatable
class A {
List<HistroyRecord> histroyRecords;
Doc currentDoc;
void update(Doc newDoc){
historyRecords.add(newDoc);
currentDoc = newDoc;
}
}
// with equatable
class B extends Equatable {
final List<HistroyRecord> histroyRecords;
final Doc currentDoc;
B copyWith({List<HistoryRecord> historyRecords,
Doc currentDoc}) {
return B(
historyRecords: historyRecords ?? this.historyRecords,
currentRecords: currentRecords ?? this.currentRecords,
}
}
Hi @duomoteng 馃憢
Thanks for opening an issue!
If you try to use bloc with mutable state you will run into problems very quickly. Does making resources immutable reduce performance? Yes, it鈥檚 more expensive to create copies than to mutate a property but I haven鈥檛 yet run into a case where the performance impact is significant enough to impact a user.
Please refer to the FAQs for more detail on this topic and let me know if you feel your application performance is significantly impacted by using immutable objects! I鈥檓 happy to take a look 馃憤
Hi, felangel, thanks for your reply. I understand why to use Equatable in state and evert.
But something that makes me confused is that when and why to use Equatable in models class. like Class Todo in the todo example.
@duomoteng did you take a look at the FAQ link I sent?
Yes, @felangel .
Can I understand as :
If there is a model class, for example Class A. if it doesn't extends Equatable, every time when it's fields are changed, the transition will happen. (Even I didn't call bloc.add(event) to trigger it)?
@duomoteng if a class doesn鈥檛 extend Equatable and you yield a new instance it will always trigger a state change even if the values of the properties were the same as before.
@felangel What if there is a field in an immutable class. That field is CacheA, which is a mutable object and need to updated frequently. Will this caused the problem we talked about before?
// without equatable
class A extends Equatable {
final CacheA cacheA;
}
}
class CacheA {
List<HistroyRecord> histroyRecords;
Doc currentDoc;
}
@duomoteng then the class is not immutable and you should avoid doing that. Yes, please avoid having non-final properties on your state classes.
Thank you @felangel !馃憤馃徎
Can you show the example if state class contains list properties then how to use with equatable
(Equatable properties should always be copied rather than modified. If an Equatable class contains a List or Map as properties, be sure to use List.from or Map.from respectively to ensure that equality is evaluated based on the values of the properties rather than the reference.)
Most helpful comment
Thank you @felangel !馃憤馃徎