等待嵌套的将来函数在Dart中完成

时间:2020-09-19 19:37:33

标签: firebase flutter dart google-cloud-firestore

当我的Future函数内部嵌套了future函数时,结果不会等待函数完成,这是从firestore获取数据的代码示例:

以前的变量:DocumentReference idoc和List sids

Future<bool> check() async {

    bool result = true;

    await idoc.get().then((value) {
      if (!value.data['av']) {
        result = false;
      } else {
        sids.forEach((sid) async {
          DocumentReference jdoc = idoc.collection('items').document(sid);
          await jdoc.get().then((jdocValue) {
            if (!jdocValue.data['av']) {
              result = false;
            }
          });
        });
      }
    });
    return result;
}  

当我调用check()时,甚至使用then:

check().then( (value) => print(value.toString()) );

如果第一个检查为true,我只是得到'false',我的意思是如果firestore中的“ idoc.data ['av']”为false,但是,如果此条件为false,并且我输入了'else',对'items'集合中的每个文档进行评估,然后如果它们中的任何一个为false,我都会调用check()获得'true'。

我想等待该函数的空洞求值,如果'items'的任何文档为假,则得到'false'以便使用它。

0 个答案:

没有答案