json_to_dart icon indicating copy to clipboard operation
json_to_dart copied to clipboard

Idea of copyWith and initial

Open UlasKasim opened this issue 6 years ago • 2 comments
trafficstars

When i am about to create a model, i write json to dart and use your repository, then add my copyWith and initial, lets say we have A model it contains

class A{
    String b;
    String c;
}

I generate your json to dart and add these

A copyWith({String b, String c}) {
    return A(
        b: b ?? this.b,
        c: c ?? this.c
        );
  }
factory A.initial(){
    return A(b:"", c:"");
}

I know that im asking this because of my lazyness but u can generate these for everyone(mostly me)

UlasKasim avatar Nov 13 '19 21:11 UlasKasim

Thanks for your contribution.

This is not hard to do but the most valuable contribution here is you use case. I've created this tool to quickly generate a parser for JSON API, but you are using this tool to actually crate classes in dart, am I right?

If this is the case, I wasn't really considering this tool for such scenario but I would like to understand why would you prefer writing JSON instead of writing the dart class. What does this tool offer that makes it more convenient for you to write JSON than plain dart?

Thanks for your feedback

javiercbk avatar Nov 15 '19 14:11 javiercbk

For me this would be fantastic in conjunction with #43 , as this is exactly how I currently have my hand written data model classes with hand written fromJson(), toJSON(), so I could then just use the convertor to completely generate all my currently hand written classes.

I think the key to this usecase is knowing that the generated classes are use as immutable data models, so having copyWith (and to lesser extent initial) would be a huge help. I suspect this would be common practise for Android developers coming from using Kotlin data classes in this manner.

maks avatar Nov 20 '19 23:11 maks