Home > Software design >  want to make a method that toggle my transaction with expense to income and vice versa in flutter
want to make a method that toggle my transaction with expense to income and vice versa in flutter

Time:12-30

I have created a method to toggle transaction type, like converting expense to income and income to expense...

and I created following method

void swapTransaction(TransactionModel transaction)
  {
    transaction.isExpense=!transaction.isExpense;
    update();
  }

it worked fine but than I was forced to apply following method,

  void swapTransaction(TransactionModel transaction)
  {
    int index=_transactions.indexOf(transaction);
    _transactions[index].isExpense=!_transactions[index].isExpense;
    update();
  }

here I want to know what is the better method to apply... is there any big difference?

CodePudding user response:

I'm not sure if such questions are suitable for this site, as it can be "opinion based" but here's my take:

I think the second method is better since you are updating the actual _transactions list, but in the first example, you are just updating what's getting passed into the function - the TransactionModel, so you're not actually changing the list of _transactions.

CodePudding user response:

According to me, second one is better. In first one you are passing one copy of object to update, which is just used within that function. While in second one you are actually updating your actual list of transaction, which you used to display.

  • Related