Open zs-dima opened 3 years ago
What is profit to have both mutable and immutable classes with identical fields?
@PiN73 For example you want to receive some object from BLoC (immutable for sure) to some Form-like, or some widgets to edit vector pictures or other specific Widgets and then modify it as Mutable object to change it's properties actively and then pass back to the BLoC (immutable for sure)
I had several cases already when it had not been possible to use immutable classes in the widgets for example.
So I had been forced to convert immutable - Mutable - immutable manually.
However it is about specific cases usages only.
Not sure if I fully understand your use case, but generally you can create new instance of freezed immutable object with some fields changed using copyWith
@PiN73 thanks for answer but I know as freezed objects could be used and using them for a long time already. 95% of time I do not need Mutable objects as I could use copyWith and etc but 5% of cases it is not possible Forms is not good example but maybe it will be easier to explain with forms: for example I have some freezed object with list of freezed objects and every list item have list of freezed objects and etc and then I bind this object to the complex form and for the Form case I working with Mutable data as user actively edit fields and lists. Then to change some sub-sub list item using copyWith - became too heavy to be able to use it (if you tried it once)
If it is mutable then we can use the same model for ObjectBox and Hive persistence.
please consider this example. (I am not sure if this is the correct way to do so)
@Entity()
@freezed
class Person with _$Person {
Person({this.id = 0, required this.firstName, required this.lastName});
}
final store = await openStore();
final box = store.box<Person>();
var person = Person(firstName: 'Joe', lastName: 'Green');
final id = box.put(person); // Create
person = box.get(id)!; // Read
person.lastName = "Black";
box.put(person); // Update
box.remove(person.id); // Delete
// find all people whose name start with letter 'J'
final query = box.query(Person_.firstName.startsWith('J')).build();
final people = query.find(); // find() returns List<Person>
@SheikhG1900 it is not related to the topic
Acceptable solution to have PersonMutable
class that mostly copy of Person
freezed Immutable class you described.
And have methods to convert Person
to and from PersonMutable
class
ObjectBox
and Hive
It is too heavy workarounds to use them and workarounds looks too ugly truly.
Think toMutable/fromMutable methods could be a very good addition
@rrousselGit , Now that we have @unfreezed do you think would be possible to have methods to convert immutable classes to/from mutable ones and vice versa?
Thanks
It is nice to use Immutable data classes to pass through bloc and atc
But it could be really nice to have Mutable data-classes version for some cases.
Could be nice to be able to generate Mutable copy for the Immutable-generated class and converters to and from this Mutable classes to the freezed Immutable classes.
for example: