异步/等待/然后在Dart/Flutter中 [英] Async/Await/then in Dart/Flutter

查看:370
本文介绍了异步/等待/然后在Dart/Flutter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flutter应用程序,我在其中使用SQFLITE插件从SQLite DB中获取数据.在这里,我面临一个奇怪的问题.据我了解,我们使用async/await或then()函数进行异步编程. 在这里,我有一个db.query()方法,该方法正在执行一些SQL查询以从数据库中获取数据.在此函数获取数据之后,我们将在.then()函数中进行一些进一步的处理.但是,以这种方式,我遇到了一些问题.从我调用此getExpensesByFundId(int fundId)函数的位置,它似乎无法正确获取数据.它应该返回Future>对象,然后在数据可用时将其转换为List.但是当我打电话时它不起作用.

I have a flutter application where I am using the SQFLITE plugin to fetch data from SQLite DB. Here I am facing a weird problem. As per my understanding, we use either async/await or then() function for async programming. Here I have a db.query() method which is conducting some SQL queries to fetch data from the DB. After this function fetches the data, we do some further processing in the .then() function. However, in this approach, I was facing some issues. From where I am calling this getExpensesByFundId(int fundId)function, it doesn't seem to fetch the data properly. It's supposed to return Future> object which will be then converted to List when the data is available. But when I call it doesn't work.

但是,我只是对其进行了一些试验,并在db.query()函数之前添加了"await"关键字,并且以某种方式使其开始可以正常工作.您能解释一下为什么添加await关键字可以解决此问题吗?我以为在使用.then()函数时,我们不需要使用await关键字.

However, I just did some experimentation with it and added "await" keyword in front of the db.query() function and somehow it just started to work fine. Can you explain why adding the await keyword is solving this issue? I thought when using .then() function, we don't need to use the await keyword.

这是我的代码:

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;
  }

推荐答案

简而言之:

await旨在中断过程流,直到异步方法完成为止. then但是不会中断处理流程(意味着将执行下一条指令),而是使您可以在异步方法完成后运行代码.

await is meant to interrupt the process flow until the async method has finished. then however does not interrupt the process flow (meaning the next instructions will be executed) but enables you to run code when the async method is finished.

在您的示例中,使用then时您无法实现所需的功能,因为代码未在等待"并且return语句已处理并因此返回空列表.

In your example, you cannot achieve what you want when you use then because the code is not 'waiting' and the return statement is processed and thus returns an empty list.

添加await时,您明确地说:直到我的Future完成(then部分)之前,不要再说了.

When you add the await, you explicitey say: 'don't go further until my Future is completed (the then part).

仅使用await,您可以编写以下代码来达到相同的结果:

You could write your code as follow to achieve the same result only with await:

Future<List<Expense>> getExpensesByFundId(int fundId) async {
    Database db = await database;

    List<Expense> expenseList = List();

    List<Map<String,dynamic>> expList = await db.query(expTable,where: '$expTable.$expFundId = $fundId');
    expList.forEach((Map<String, dynamic> expMap) {
        expenseList.add(Expense.fromMap(expMap));
    });

    return expenseList;
}

您还可以选择仅使用then部分,但是您需要确保随后正确调用getExpensesByFundId:

You could also chose to use only the then part, but you need to ensure to call getExpensesByFundId properly afterward:

Future<List<Expense>> getExpensesByFundId(int fundId) async {
    Database db = await database;

    List<Expense> expenseList = List();

    return db.query(expTable,where: '$expTable.$expFundId = $fundId')
        .then((List<Map<String,dynamic>> expList){
      expList.forEach((Map<String, dynamic> expMap){
        expenseList.add(Expense.fromMap(expMap));
      });
    });
}

// call either with an await
List<Expense> list = await getExpensesByFundId(1);
// or with a then (knowing that this will not interrupt the process flow and process the next instruction
getExpensesByFundId(1).then((List<Expense> l) { /*...*/ });

这篇关于异步/等待/然后在Dart/Flutter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆