Updating of deeply nested children of the data model
Since you are using Freezed, this should not be a problem.
Freezed offers a built-in mechanism for updating deeply nested variables
https://github.com/rrousselGit/freezed#deep-copy
For example, assume that you have:
@freezed
abstract class Company with _$Company {
factory Company({String name, Director director}) = _Company;
}
@freezed
abstract class Director with _$Director {
factory Director({String name, Assistant assistant}) = _Director;
}
@freezed
abstract class Assistant with _$Assistant {
factory Assistant({String name, int age}) = _Assistant;
}
Then that instead of:
Company company;
Company newCompany = company.copyWith(
director: company.director.copyWith(
assistant: company.director.assistant.copyWith(
name: 'John Smith',
),
),
);
You can write:
Company company;
Company newCompany = company.copyWith.director.assistant(name: 'John Smith');
Deep copying of whole data even on a minor change in any of the nested data model(Like a letter change in the google doc), would make our application slow.
When using immutability, there are very few reasons to make a "deep copy".
Instead it is a shallow copy that is performed.
To continue on the Company example, assume that you have:
var company = Company(
name: 'Google',
director: Director(
name: 'John',
),
)
Then when you want to change the company name, you'll only need to do:
company = company.copyWith(name: 'Facebook');
When doing so, the Director is not recreated. Only the Company is, which is a lot more efficient.
It's fine since Director is immutable. We can't change anything on Director, so there is no risk of conflict even if two objects are using the same Director.
We were planning to keep multiple copy of the the same model for supporting the undo or redo, but this will take a lot of memory space of the end user.
Continuing from the previous point, what you will store is not deep copies, but shallow copies.
So in the end the memory impact is reduced by a lot.
And as you mentioned undo-redo, even if you didn't use immutable data, chances are you'd still use that memory.