Open vasilich6107 opened 4 years ago
I just realized that SomeCalss.fromJson(someClass.toJson())
could be some kind of a trick to make a copy)
But you'd have to serialize and deserialize it again. copyWith
would be more performatic.
@comigor what do you think about this https://pub.dev/packages/freezed ?
I've personally never used it, but it seems nice. I liked the future-proof for non-nullables and late variables!
+1. My use case is a search/filter with several parameters being passed to a query. I have three different fields updating the variable at different times (textfield, filters, sorting) and as options are selected in any of these I want to update the global variable that is located in the service with these parameters. I can't use the Artemis generated type from the graphql query itself as it's immutable. In the end I created another type that has a copyWith method which is largely identical to the Artemis generated type.
Happy to be corrected if there is a better way to do this.
@comigor what do you think about this https://pub.dev/packages/freezed ?
I strongly recommend this
is there an update on this issue? I am using fragments that create mixins thus fromJson and toJson methods are not available.
is there any way I can add let's say https://pub.dev/packages/freezed manually ? @vasilich6107
No progress for now
If made this extension method, if anyone looking to do something similar.
extension NewInstance on ClassMixin {
ClassMixin jsonCopyWith() {
return Class.fromJson(
(this as Class).toJson());
}
}
@vasilich6107 freezed
would be a great addition and it would simplify a little bit the needed dependencies: json_annotation
and equatable
wouldn't be needed because freezed_annotation
already exports json_annotation
and already has equality check :). Also it has a nice toString()
method implementation :)
It would be nice to have this implemented on the generated fragment mixins as well.
Is this still in consideration? It would be nice to have copyWith method in the generated classes.
As far as dart passes objects by reference it would be great to have a 'copyWith' extension for the data classes with something like this
https://github.com/numen31337/copy_with_extension
docs https://www.oleksandrkirichenko.com/blog/dart-extensions/