Freezed: Add method to generate mutable version of the class

Created on 23 May 2020  路  4Comments  路  Source: rrousselGit/freezed

Hi, thanks a lot for your effort creating this project. I have a proposal to add new feature.

@freezed
abstract class MyClass with _$MyClass {
  const factory MyClass({int students}) = _MyClass;
  const factory MyClass.fromJson(Map<String, dynamic> json) => _$MyClassFromJson(json);
}

then one can create mutable version

final immutableClass = MyClass(students: 20);
final mutableClass = immutableClass.toMutable();
mutableClass.students = 50;
final immutableClass2 = mutableClass.toImmutable();
final json1 = mutableClass.toJson();
final json2 = immutableClass2.toJson();
assert(json1 == json2);

similiar to built_value toBuilder()

enhancement

Most helpful comment

Another feature request, I think toJson() implmentation shouldn't be force autogenerated, but user should has the option to write it manually like @Freezed(generateToJson: false).

For instance, my app is written in flutter and angulardart. I use firestore to store database. Firestore timestamp implementation is different in web and flutter, so do many others database have their implementation of DateTime. So my data model implement toJson(TimestampConverter converter) and factory Model.fromJson(Map<String,dynamic> json, TimestampConverter converter) that it can be used either in flutter or angulardart. And I can use single data model both in angulardart and flutter by providing each timestamp converter. Currently using freeezed, I use method toJsonWithConverter(TimestampConverter converter).

All 4 comments

Another feature request, I think toJson() implmentation shouldn't be force autogenerated, but user should has the option to write it manually like @Freezed(generateToJson: false).

For instance, my app is written in flutter and angulardart. I use firestore to store database. Firestore timestamp implementation is different in web and flutter, so do many others database have their implementation of DateTime. So my data model implement toJson(TimestampConverter converter) and factory Model.fromJson(Map<String,dynamic> json, TimestampConverter converter) that it can be used either in flutter or angulardart. And I can use single data model both in angulardart and flutter by providing each timestamp converter. Currently using freeezed, I use method toJsonWithConverter(TimestampConverter converter).

Could you make that other request as a separate issue? It is unrelated to this issue

Done. Sorry for the late reply.

Closing this issue as I don't plan to support mutability on Freezed

Was this page helpful?
0 / 5 - 0 ratings