Skip to content

add copyWith method to models #403

Description

@kidusdev

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions