Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add copyWith method to models #403

Open
Tracked by #431
kidusdev opened this issue Jun 23, 2024 · 3 comments
Open
Tracked by #431

add copyWith method to models #403

kidusdev opened this issue Jun 23, 2024 · 3 comments

Comments

@kidusdev
Copy link

kidusdev commented Jun 23, 2024

Hy

I encounter this redundant code in various places of my application

final e = someMonthlyExpense;

 Expenses(
      id: e.amount == amount ? e.id : 0,
      amount: e.amount/30,
      period: e.period,
      reason: e.reason,
      startDate: e.startDate,y
      endDate: e.endDate,
      createdAt: e.createdAt,
    );

here in the above example i am grouping expenses to their lower expense period. its like if i have 1500$ house rent expense and if i have to cover that in a month how much should i earn daily and its obviously 50$ and the same for yearly expense. and the new value is treated as daily expense and the only difference is the amount. it would be great if we can do this instead;

final e = someMonthlyExpense;

final dailyExpense = e.copyWith(amount: e.amount/30);
// this ^ is better and concise code than this
/**
 Expenses(
      id: e.amount == amount ? e.id : 0,
      amount: e.amount/30,
      period: e.period,
      reason: e.reason,
      startDate: e.startDate,y
      endDate: e.endDate,
      createdAt: e.createdAt,
    );
*/

i tried

Expenses.fromJson({...e.toJson(), "amount": e.amount! * difference});

it doesn't work because the toJson method changes the createdAt to String then when i use the fromJson method is complains type 'String' is not a subtype of type 'DateTime?'. the DateTime Field in the models should parse a String and DateTime types

NB. i added the copyWith method to the models but it resets everytime i change the schema.prisma and generate a client

@medz
Copy link
Owner

medz commented Jun 24, 2024

I'll take some time to look at it. In fact, another problem has arisen. FromJson does not handle time strings correctly.

@kidusdev
Copy link
Author

another problem has arisen. FromJson does not handle time strings correctly.

Yeah, i encountered that many time i added an extra step to parse string to dates and assigning it to the model in my apps before mentioning it here.

@medz
Copy link
Owner

medz commented Jun 24, 2024

@kidusdev Fixed an issue with <model>.fromJson not being able to handle DateTime.

I actually want to wait for the macros (which are coming soon) to add the copyWith feature

Because along with that, we should not only add fromJson/toJson/copyWith to model, but also to input types. So that types can be published independently for the front end to use it.

@medz medz mentioned this issue Sep 26, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants