Populate and return custom data type from a custom action

Custom Code

Hi All,

I am trying to return a list of custom data type 'TransactionTotals' from a custom action. For now I am just trying to add a single TransactionTotals row to my TransactionTotals list 'TransactionsTotalsDT', using this code below:

   TransactionsTotalsDT.addAll([
      TransactionTotalsStruct(
          Type: "Expense",
          Category: "Shopping",
          Subcategory: "Home",
          Currency: "GBP",
          Amount: 899)
    ]);

When I compile the code I get the error messages below. Can anyone explain how to create a List of a custom data type in a custom action and populate it with custom data type rows?

Error Messages:

Full Code

/ Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

Future<List<TransactionTotalsStruct>?> groupTransactionsv2(
  List<TransactionRecord>? transactionlist,
  List<UsercategoriesRecord>? catlist,
  List<UsersubcategoriesRecord>? subcatlist,
) async {
  // Add your function code here!

  // Map<String, Map<String, double>> transactiontotals = {};
  //Map<String, double> subcattotals = {};

  Map<String, Map<String, Map<String, double>>> transactiontotals = {};
  List<TransactionTotalsStruct> TransactionsTotalsDT = [];

  String type;
  String subcatname;
  String catname;
  double amount;

  if (transactionlist != null) {
    for (var record in transactionlist) {
      catname = searchCategory(catlist, record.category ?? null);
      subcatname = searchSubCategory(subcatlist, record.subcategory ?? null);
      type = record.type;

      amount = record.amount;

      if (type == null) {
        type = '';
      }

      if (catname == null) {
        catname = '';
      }

      if (subcatname == null) {
        subcatname = '';
      }

      if (transactiontotals.containsKey(type) == false) {
        transactiontotals[type] = {};
      }

      if (transactiontotals[type]!.containsKey(catname) == false) {
        transactiontotals[type]![catname] = {};
      }

      if (transactiontotals[type]![catname]!.containsKey(subcatname) == false) {
        transactiontotals[type]![catname]![subcatname] = 0;
      }

      transactiontotals[type]![catname]![subcatname] =
          (transactiontotals[type]![catname]![subcatname] ?? 0) + amount;

      //subcattotals[subcatname] =  //TS my old code, this works
      //(subcattotals[subcatname] ?? 0) + record.amount;
    }
    //return subcattotals.length;
    //String jsonTransaction = jsonEncode(subcattotals);
    String jsonTransaction = jsonEncode(transactiontotals);

    //Map<String, dynamic> tempdata;
    //tempdata = {
    //  "Type": "Expense",
    //  "Category": "Shopping",
    //  "Subcategory": "Home",
    // "Currency": "GBP",
    //  "Amount": 899
    //};

    TransactionsTotalsDT.addAll([
      TransactionTotalsStruct(
          Type: "Expense",
          Category: "Shopping",
          Subcategory: "Home",
          Currency: "GBP",
          Amount: 899)
    ]);

    //return jsonTransaction;
    return TransactionsTotalsDT;
  }
}

Custom Data Type

What have you tried so far?
Did you check FlutterFlow's Documentation for this topic?
Yes
3
5 replies