Getting a "this function is empty or cannot be parsed" error for a custom function in flutterflow

953 views Asked by At

I am trying to write a custom function in flutterflow that would allow me to figure out the who owes who how much based on their expenses that is saved in a firebase document.

the firebase document is called transactions, and I first make a dart Map with key:value pairs consisted of each member and the sum of each member's expenses.

the rest of the code uses the splitwise algorithm recommended by chatgpt. It seems that there are no syntax errors, but for some reason I am getting a "this function is empty or cannot be parsed" error. The following is my code

List<String> calculatedebtors(List<TransactionsRecord> transactions)  {
  /// MODIFY CODE ONLY BELOW THIS LINE

import 'dart:collection';

class Debt {
  String debtor;
  String creditor;
  double amount;

  Debt(this.debtor, this.creditor, this.amount);
}

  Map<String, double> transactionMap = {};

  // Iterate through the transactions and calculate sums
  for (var transaction in transactions) {
    if (transaction.type == false) {
      String member = transaction.payer;
      double expense = transaction.amount;

      // Update the sum for the payer
      transactionMap[member] = (transactionMap[member] ?? 0) + expense;
    }
  }

  Map<String, double> balances = {}..addAll(transactionMap);

  List<Debt> debts = [];

  while (balances.isNotEmpty) {
    var creditor = balances.keys.first;
    var debtor = balances.keys.last;
    var creditorBalance = balances[creditor];
    var debtorBalance = balances[debtor];

    var minAmount = creditorBalance.abs() < debtorBalance.abs()
        ? creditorBalance.abs()
        : debtorBalance.abs();

    var debt = Debt(debtor, creditor, minAmount);

    if (creditorBalance < 0) {
      creditorBalance += minAmount;
    } else {
      balances.remove(creditor);
    }

    if (debtorBalance > 0) {
      debtorBalance -= minAmount;
    } else {
      balances.remove(debtor);
    }

    if (debtorBalance != 0) {
      balances[debtor] = debtorBalance;
    }

    if (creditorBalance != 0) {
      balances[creditor] = creditorBalance;
    }

    debts.add(debt);
  }

  List<String> debtorsList = [];
  for (var debt in debts) {
    debtorsList.add(debt.debtor);
  }

  return debtorsList;
  /// MODIFY CODE ONLY ABOVE THIS LINE
}

Below is a screenshot for the argument settings flutterflow custom function argument settings

I need help. I tried mostly everything I know, but do consider I am very new to this field and rely heavily on tools such as chatgpt.

1

There are 1 answers

1
Ivo On

The code you got should be identical to this:

List<String> calculatedebtors(List<TransactionsRecord> transactions) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  Map<String, double> transactionMap = {};

  // Iterate through the transactions and calculate sums
  for (var transaction in transactions) {
    if (transaction.type == false) {
      String member = transaction.payer;
      double expense = transaction.amount;

      // Update the sum for the payer
      transactionMap[member] = (transactionMap[member] ?? 0) + expense;
    }
  }

  Map<String, double> balances = {}..addAll(transactionMap);

  List<String> debtorsList = [];

  while (balances.isNotEmpty) {
    var creditor = balances.keys.first;
    var debtor = balances.keys.last;
    var creditorBalance = balances[creditor]!;
    var debtorBalance = balances[debtor]!;

    var minAmount = creditorBalance.abs() < debtorBalance.abs()
        ? creditorBalance.abs()
        : debtorBalance.abs();

    if (creditorBalance < 0) {
      creditorBalance += minAmount;
    } else {
      balances.remove(creditor);
    }

    if (debtorBalance > 0) {
      debtorBalance -= minAmount;
    } else {
      balances.remove(debtor);
    }

    if (debtorBalance != 0) {
      balances[debtor] = debtorBalance;
    }

    if (creditorBalance != 0) {
      balances[creditor] = creditorBalance;
    }

    debtorsList.add(debtor);
  }

  return debtorsList;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

It's actually a mystery to me why there even was a Debt class since only the debtor was every used of it, so might as well just work directly with the debtorsList. I haven't really looked into the algorithm itself so no idea if that one is even correct. In any case I wouldn't just copy code from ChatGPT without understanding what's even happening there.