【发布时间】:2019-06-28 03:17:36
【问题描述】:
我有一个颤振应用程序,我在其中使用 SQFLITE 插件从 SQLite DB 获取数据。在这里,我面临一个奇怪的问题。据我了解,我们使用 async/await 或 then() 函数进行异步编程。 这里我有一个 db.query() 方法,它执行一些 SQL 查询以从数据库中获取数据。在这个函数获取数据之后,我们在 .then() 函数中做一些进一步的处理。但是,在这种方法中,我遇到了一些问题。从我调用这个 getExpensesByFundId(int fundId) 函数的地方,它似乎无法正确获取数据。它应该返回 Future> 对象,然后在数据可用时将其转换为 List 。但是当我打电话时它不起作用。
但是,我只是对它进行了一些实验,并在 db.query() 函数前面添加了“await”关键字,不知何故它刚刚开始正常工作。你能解释一下为什么添加 await 关键字可以解决这个问题吗?我想在使用 .then() 函数时,我们不需要使用 await 关键字。
这是我的代码:
Future<List<Expense>> getExpensesByFundId(int fundId) async {
Database db = await database;
List<Expense> expenseList = List();
// The await in the below line is what I'm talking about
await db.query(expTable,where: '$expTable.$expFundId = $fundId')
.then((List<Map<String,dynamic>> expList){
expList.forEach((Map<String, dynamic> expMap){
expenseList.add(Expense.fromMap(expMap));
});
});
return expenseList;
}
【问题讨论】:
标签: flutter dart asynchronous async-await