yoohoo
yoohoo

Reputation: 1217

pass different class object to function

I have multiple class objects and i want to create a standalone function that will take any class object as parameter.

here is my income class


    import 'package:finsec/model/dao.dart';

    class Income {
      int id, groupId, weekNumber, monthNumber, calendarYear;
      String frequency, payoutDate, category, depositTo, description, status;
      double expectedAmount, actualAmount;


      Income(
        this.id,
        this.groupId,
        this.expectedAmount,
        this.actualAmount,
        this.frequency,
        this.payoutDate,
        this.category,
        this.depositTo,
        this.status,
        this.weekNumber,
        this.monthNumber,
        this.calendarYear,
        [this.description]
      );
    }

here is my expense class object


    import 'package:finsec/model/dao.dart';

    class Expense{
      int id, groupId
      String frequency, paidDate, expCategory,  description, status;
      double expectedAmount, actualAmount;


      Expense(
        this.id,
        this.groupId,
        this.expectedAmount,
        this.actualAmount,
        this.frequency,
        this.paidDate,
        this.expCategory,
        this.status,
        [this.description]
      );
    }


// The base class for the different types of items the list can contain.

    abstract class Dao<T> {
      //queries
      String get createTableQuery;

      //abstract mapping methods
      T fromMap(Map<String, dynamic> query);
      List<T> fromList(List<Map<String,dynamic>> query);
      Map<String, dynamic> toMap(T object);
    }


    import 'package:finsec/model/dao.dart';
    import 'package:finsec/model/income/income.dart';

    class IncomeDao implements Dao<Income> {

      @override
      Income fromMap(Map<String, dynamic> query) {
        Income income = Income();
        income.id = query["id"];
        income.groupId = query["groupId"];
        income.amount = query["amount"];
        income.frequency = query["frequency"];
        income.payoutDate = query["payoutDate"];
        income.category = query["category"];
        income.depositTo = query["depositTo"];
        income.status = query["status"];
        return income;
      }

      @override
      Map<String, dynamic> toMap(Income object) {
        return <String, dynamic>{
          "id": object.id,
          "groupId": object.groupId,
          "amount": object.amount,
          "frequency": object.frequency,
          "payoutDate": object.payoutDate,
          "category": object.category,
          "depositTo": object.depositTo,
          "status": object.status,
        };
      }

      @override
      List<Income> fromList(List<Map<String,dynamic>> query) {
        List<Income> income = List<Income>();
        for (Map map in query) {
          income.add(fromMap(map));
        }
        return income;
      }
    }

i want to create function that takes any class object

 Future<int> insertTransaction(T object) async {
    var dao = new IncomeDao();
      dao.toMap(object));  
  }

basically, i want to be able to call insertTransaction and pass any class object and then pass that object to the dao class. when i called dao.toMap(object)); in insertTransaction function. i get error message such as "The argument type 'T' can't be assigned to the parameter type 'Income'."

I guess flutter is not able to determine if object parameter is Income or expense etc. i tried using casting such as (Income)object but didnt work.

can someone help me on this? im trying to reuse my function (insertTransaction) instead of creating the same function for every object class such as income, expenses, events, etc...

Upvotes: 2

Views: 475

Answers (1)

ejabu
ejabu

Reputation: 3147

just change T to dynamic

insertTransaction(dynamic object)

Upvotes: 1

Related Questions