Json_serializable.dart: Possible to add CopyWith to generated code?

Created on 19 Jun 2019  路  2Comments  路  Source: google/json_serializable.dart

Let me preface this by saying that I'm near the end of a rabbit hole I think others might follow.

Coming from a React world, I decided on flutter_redux for state management. Immutability, right? However, the boilerplate was too much and I decided to move to the officially backed provider library. It's great except there aren't many examples of how best to update a model instance (populated from an API) that is provided by provider. Remi recommends immutability but doesn't name anything specific.

Again, from React, I decided on built_value and built_collection for value types that I could update through the builder pattern. But the builder pattern felt odd and so did the fact that built_collection didn't explicitly allow nulls. I get why nulls are bad but it was weird to see when everywhere else in Flutter, nulls are still commonly used. I also saw that json_serializable was the example used on the Flutter page about serialization and therefore must be the recommended option.

I switched and actually prefer json_serializable for it's simplicity.

TL;DR:
I'd be nice if the generated code could automate creating copyWith factories as that's what someone who writes Flutter is used to using in other objects. There is an open issue at built_value that is similar. I don't know if this is out of this project's scope but I think it'd be interesting to see. (Currently, I'm implementing all of the copyWith's by hand )

Most helpful comment

I want this feature, too 鈥撀燽ut this in not the right repository.

Maybe something to look into with https://github.com/kevmoo/build_compare 鈥撀爃rm...

All 2 comments

I want this feature, too 鈥撀燽ut this in not the right repository.

Maybe something to look into with https://github.com/kevmoo/build_compare 鈥撀爃rm...

@Nolence I recently wrote to the library to deal with this issue, which is probably enough for most cases https://pub.dev/packages/copy_with_extension_gen

Was this page helpful?
0 / 5 - 0 ratings