[英]How to Retrieve Flutter Dart List Value from List Using List Value Foreign Key id from Second List
預先感謝您的幫助。 我的 flutter 應用程序有一個 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 及其相關的方法和屬性感到很糟糕。
您可能必須編寫一個輔助函數,該函數循環遍歷“accounts”和“budgets”以分別匹配Account_id
和Budget_id
,然后從與 id 匹配的 Account 和 Budget 中獲取名稱。 像這樣的東西:
String getAccountName(acc_id_in_selected_transaction){
for (Account a in accounts){
if(a.Account_Id == acc_id_in_selected_transaction){
return a.AccountName;
}
}
}
在這里, acc_id_in_selected_transaction
是transactions[i]['Account_id']
。
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.