【发布时间】:2020-10-28 08:19:06
【问题描述】:
提前感谢您的帮助。我的颤振应用程序有一个 sqlite 数据库,其中包含一些表。我的一个表包含两个外键,它们是来自其他两个表主键的 id。
//Create Tables
await db
.execute('CREATE TABLE Account(id INTEGER PRIMARY KEY, AccountName TEXT NOT NULL, Icon INTEGER NOT NULL, CurrencyType TEXT NOT NULL, ' + 'Balance REAL NOT NULL, ' + 'IsPrimary TEXT NULL)');
await db.execute('CREATE TABLE Budget(id INTEGER PRIMARY KEY, BudgetName TEXT NOT NULL, Icon INTEGER NOT NULL, Amount REAL NOT NULL, ' + 'IsPrimary TEXT NULL)');
await db.execute('CREATE TABLE Transactions(id INTEGER PRIMARY KEY, Account_id INTEGER NOT NULL, ' +
'Budget_id INTEGER NOT NULL, Icon INTEGER NOT NULL, Type TEXT NOT NULL,Amount REAL NOT NULL, Date TEXT NOT NULL, ' +
'Party TEXT NOT NULL, Note TEXT NULL, ' +
'FOREIGN KEY(Account_id) REFERENCES Account(id), ' +
'FOREIGN KEY(Budget_id) REFERENCES Budget(id))');
//Retrieve Data
Future<List<Account>> getAccounts() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query('Account', where: 'id NOT IN (1)');
return List.generate(maps.length, (i) {
return Account(
maps[i]['id'],
maps[i]['AccountName'],
maps[i]['Icon'],
maps[i]['CurrencyType'],
maps[i]['Balance'],
maps[i]['IsPrimary'],
);
});
}
Future<List<Budget>> getBudgets() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query('Budget', where: 'id NOT IN (1,2)');
return List.generate(maps.length, (i) {
return Budget(
maps[i]['id'],
maps[i]['BudgetName'],
maps[i]['Icon'],
maps[i]['Amount'],
maps[i]['IsPrimary'],
);
});
}
Future<List<Transactions>> getTransactions() async {
final db = await database;
final List<Map<String, dynamic>> maps = await db.query('Transactions');
return List.generate(maps.length, (i) {
return Transactions(
maps[i]['id'],
maps[i]['Type'],
maps[i]['Amount'],
maps[i]['Party'],
maps[i]['Date'],
maps[i]['Budget_id'],
maps[i]['Account_id'],
maps[i]['Note'],
maps[i]['Icon'],
);
});
}
//Variables and Method in my Stateful Widget
List<Transactions> transactions = List<Transactions>();
List<Account> accounts = List<Account>();
List<Budget> budgets = List<Budget>();
Future showTransactions() async {
accounts = await DbHelper.db.getAccounts();
transactions = await DbHelper.db.getTransactions();
budgets = await DbHelper.db.getBudgets();
setState(() {
transactions = transactions;
accounts = accounts;
budgets = budgets;
});
}
我有一个显示输入交易的屏幕,但在文本小部件中显示与交易相关的 AccountName 和 BudgetName 时遇到问题。
有没有一种方法可以使用交易列表中的 Account_id 和 Budget_id 外键从我在交易屏幕中初始化的账户和预算列表中检索 AccountName 和 BudgetName?还是我需要创建另一个查询来执行此操作?我在 ListView.Builder 中有交易。我在检索交易数据时没有问题,但只需要使用外键从其他列表中获取帐户和预算名称。
再次感谢您的帮助。我对 Lists 和 Maps 及其相关的方法和属性感到很糟糕。
【问题讨论】:
标签: list sqlite dictionary flutter dart