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
Hy
I encounter this redundant code in various places of my application
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;
i tried
it doesn't work because the
toJsonmethod changes thecreatedAttoStringthen when i use thefromJsonmethod is complainstype 'String' is not a subtype of type 'DateTime?'. the DateTime Field in the models should parse aStringandDateTimetypesNB. i added the
copyWithmethod to the models but it resets everytime i change the schema.prisma and generate a client