我有多个类对象,我想创建一个将任何类对象作为参数的独立函数。
这是我的收入阶层
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]
);
}
这是我的费用类别对象
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;
}
}
我想创建可以接收任何类对象的函数
Future<int> insertTransaction(T object) async {
var dao = new IncomeDao();
dao.toMap(object));
}
基本上,我希望能够调用insertTransaction并传递任何类对象,然后将该对象传递给dao类。当我打电话给dao.toMap(object));在insertTransaction函数中。我收到错误消息,例如“无法将参数类型'T'分配给参数类型'收入'。”
我猜flutter无法确定对象参数是收入还是费用等。我尝试使用诸如(收入)对象的转换,但没有用。
有人可以帮我吗?我试图重用我的函数(insertTransaction),而不是为每个对象类(例如收入,支出,事件等)创建相同的函数。
答案 0 :(得分:0)
只需将T更改为动态
public async Task GetCustomerAndRidesById_When_MultipleCustomersArePresent()
{
var connectionStringBuilder =
new SqliteConnectionStringBuilder { DataSource = ":memory:" };
var connection = new SqliteConnection(connectionStringBuilder.ToString());
var options = new DbContextOptionsBuilder<TravelContext>()
.UseSqlite(connection)
.Options;
var customer = _customerBuilder.WithDefaults();
Customer customerFromRepo;
using (var context = new TravelContext(options))
{
context.Database.OpenConnection();
context.Database.EnsureCreated();
await context.Customers.AddAsync(customer);
await context.SaveChangesAsync();
_output.WriteLine($"Customer ID: {customer.Id}");
//var customerRepository = new CustomerRepository(context);
//customerFromRepo = await customerRepository.GetByIdWithRidesAsync(customer.Id);
}
using (var context = new TravelContext(options))
{
var customerRepository = new CustomerRepository(context);
try
{
customerFromRepo = await customerRepository.GetByIdWithRidesAsync(customer.Id);
}
catch (System.Exception e)
{
throw;
}
}
customerFromRepo.Should().BeEquivalentTo(customer);
customerFromRepo.Rides.Should().HaveCount(customer.Rides.Count);
}