fetchData method

Future<void> fetchData()

Implementation

Future<void> fetchData() async {
  setState(() {
    loading = true;
  });

  List<TransactionModel> balances = await _wapasPlugin.getBalanceReport(
    widget.userId,
    widget.currency,
    widget.startDate,
    widget.endDate,
    widget.group,
    widget.includePrevious,
    widget.includeToday,
  );

  if (balances.length == 2) {
    balances.forEach((balance) {
      if (formatDate(balance.date) == formatDate(widget.endDate)) {
        setState(() {
          transaction = balance.toJson()[widget.volume == 'group'
                  ? 'groupedTransactions'
                  : 'totalTransactions']?[widget.transactionType] ??
              0;
          if (widget.type == "amount") {
            if (widget.amountType == "virtual") {
              amount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedVirtualValues'
                          : 'totalVirtualValues']?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              amount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedAmounts'
                          : 'totalAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          } else if (widget.type == "credit") {
            if (widget.amountType == "virtual") {
              amount = (balance.toJson()[widget.volume == 'group'
                              ? 'groupedCrediVirtualValues'
                              : 'totalCrediVirtualValues']
                          ?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              amount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedCreditAmounts'
                          : 'totalCreditAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          } else if (widget.type == "debit") {
            if (widget.amountType == "virtual") {
              amount = (balance.toJson()[widget.volume == 'group'
                              ? 'groupedDebitVirtualValues'
                              : 'totalDebitVirtualValues']
                          ?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              amount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedDebitAmounts'
                          : 'totalDebitAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          }
        });
      } else {
        setState(() {
          previousTransaction = balance.toJson()[widget.volume == 'group'
                  ? 'groupedTransactions'
                  : 'totalTransactions']?[widget.transactionType] ??
              0;
          if (widget.type == "amount") {
            if (widget.amountType == "virtual") {
              preAmount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedVirtualValues'
                          : 'totalVirtualValues']?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              preAmount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedAmounts'
                          : 'totalAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          } else if (widget.type == "credit") {
            if (widget.amountType == "virtual") {
              preAmount = (balance.toJson()[widget.volume == 'group'
                              ? 'groupedCrediVirtualValues'
                              : 'totalCrediVirtualValues']
                          ?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              preAmount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedCreditAmounts'
                          : 'totalCreditAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          } else if (widget.type == "debit") {
            if (widget.amountType == "virtual") {
              preAmount = (balance.toJson()[widget.volume == 'group'
                              ? 'groupedDebitVirtualValues'
                              : 'totalDebitVirtualValues']
                          ?[widget.transactionType] ??
                      0)
                  .toDouble();
            } else {
              preAmount = (balance.toJson()[widget.volume == 'group'
                          ? 'groupedDebitAmounts'
                          : 'totalDebitAmounts']?[widget.transactionType] ??
                      0)
                  .toDouble();
            }
          }
        });
      }
    });
  } else if (balances.length == 1) {
    setState(() {
      transaction = 0;
      previousTransaction = balances[0].toJson()[widget.volume == 'group'
              ? 'groupedTransactions'
              : 'totalTransactions']?[widget.transactionType] ??
          0;
      amount = 0;
      if (widget.type == "amount") {
        if (widget.amountType == "virtual") {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedVirtualValues'
                      : 'totalVirtualValues']?[widget.transactionType] ??
                  0)
              .toDouble();
        } else {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedAmounts'
                      : 'totalAmounts']?[widget.transactionType] ??
                  0)
              .toDouble();
        }
      } else if (widget.type == "credit") {
        if (widget.amountType == "virtual") {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedCrediVirtualValues'
                      : 'totalCrediVirtualValues']?[widget.transactionType] ??
                  0)
              .toDouble();
        } else {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedCreditAmounts'
                      : 'totalCreditAmounts']?[widget.transactionType] ??
                  0)
              .toDouble();
        }
      } else if (widget.type == "debit") {
        if (widget.amountType == "virtual") {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedDebitVirtualValues'
                      : 'totalDebitVirtualValues']?[widget.transactionType] ??
                  0)
              .toDouble();
        } else {
          preAmount = (balances[0].toJson()[widget.volume == 'group'
                      ? 'groupedDebitAmounts'
                      : 'totalDebitAmounts']?[widget.transactionType] ??
                  0)
              .toDouble();
        }
      }
    });
  } else {
    setState(() {
      transaction = 0;
      previousTransaction = 0;
      amount = 0;
      preAmount = 0;
    });
  }

  setState(() {
    loading = false;
  });
}