I am new to Dart/Flutter and try to use the Bloc framework for my current project. I am now at the point, where the data, especially in states, is getting more complex.

For example: A certain state holds a big list of items. The items itself holding other items which are holding certain properties etc.

From other programming language i am used to modify those objects/properties by just iterating over the collection and modify the certain property when needed.

If i got it right, the concept of "immutability" means to exactly NOT doing this. Instead, i create/copy every single nested object or property from the origin to a new object.

In the bloc framework - especially states - must be immutable. This is where it feels to me very contra-intuitive. I enforce immutability, even if the data is actually mutated very often (adding list entries, change a property from a to b). This seems to me very odd.

Consider a state where i have 1000 list entries to be shown in a table. Each of them has a property "isSelected" ... for one of those entries i wanna change "isSelected" from "false" to "true", since the user selected it in frontend ... do i have to create/copy every object to a new list now to complete the whole state again? If i got it right, i have to.

I have two issues with this:

  1. its very laborious to create such "new" objects with just one or few changes, code complexity will raise and readability will get worse (?) I fear there will be tons of code just for assembling/disassembling objects

  2. it will have performance impacts, that is growing with the size of the object (?)

Do i miss something here? How do you solve this in a smart way? I know, there are these copyFrom methods, but they will not prevent performance issues.

Whats the point of making everything immutable?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.