【问题标题】:How to Retrieve Flutter Dart List Value from List Using List Value Foreign Key id from Second List如何使用第二个列表中的列表值外键 id 从列表中检索 Flutter Dart 列表值
【发布时间】: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


    【解决方案1】:

    您可能必须编写一个辅助函数,该函数循环遍历“accounts”和“budgets”以分别匹配 Account_idBudget_id,然后从 Account 和 Budget 中获取与 id 匹配的名称。像这样的:

    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_transactiontransactions[i]['Account_id']

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的快速响应和帮助。我的大脑一直在试图解决这个问题。我今晚会试一试,然后告诉你进展如何。
    • 我无法编辑我上面的评论,所以这条评论扩展了上一条。我在想我必须以某种方式一起使用 indexOf()、elementAt() 或 where()。我尝试了所有这些时髦的变化,但没有运气。我希望避免每个索引都出现循环,但这可能是我唯一的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-04-11
    • 2019-09-27
    • 2020-04-08
    • 2019-11-21
    • 2021-11-23
    • 2021-06-10
    相关资源
    最近更新 更多