artemis icon indicating copy to clipboard operation
artemis copied to clipboard

Add copyWith methods generation to objects

Open vasilich6107 opened this issue 4 years ago • 12 comments

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/

vasilich6107 avatar Mar 24 '20 12:03 vasilich6107

I just realized that SomeCalss.fromJson(someClass.toJson()) could be some kind of a trick to make a copy)

vasilich6107 avatar Apr 04 '20 15:04 vasilich6107

But you'd have to serialize and deserialize it again. copyWith would be more performatic.

comigor avatar Apr 04 '20 18:04 comigor

@comigor what do you think about this https://pub.dev/packages/freezed ?

vasilich6107 avatar May 31 '20 20:05 vasilich6107

I've personally never used it, but it seems nice. I liked the future-proof for non-nullables and late variables!

comigor avatar Jun 01 '20 14:06 comigor

+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.

FickleLife avatar Jul 07 '20 06:07 FickleLife

@comigor what do you think about this https://pub.dev/packages/freezed ?

I strongly recommend this

joelpinto avatar Nov 02 '20 10:11 joelpinto

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

ramyak-mehra avatar May 10 '21 01:05 ramyak-mehra

No progress for now

vasilich6107 avatar May 10 '21 06:05 vasilich6107

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());
  }
}

ramyak-mehra avatar May 10 '21 13:05 ramyak-mehra

@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 :)

fpabl0 avatar May 11 '21 20:05 fpabl0

It would be nice to have this implemented on the generated fragment mixins as well.

JulianBissekkou avatar Jul 09 '21 13:07 JulianBissekkou

Is this still in consideration? It would be nice to have copyWith method in the generated classes.

salihagic avatar Oct 24 '21 01:10 salihagic