简体   繁体   English

无法从 firebase Stream 查询返回 object 的列表

[英]Unable to return list of object from firebase Stream query

I am making a firebase query which must return a list of object as stream.So to achieve that I am using Async* to wait for my loop for to complete before continueing the process.我正在进行 firebase 查询,该查询必须返回 object 的列表作为 stream。因此,为了实现这一点,我使用Async*等待我的循环for ,然后再继续该过程。 But the problem is that, my function stopped in If condition and does not enter inside his for loop and my function does not return anything.但问题是,我的 function 在If条件下停止并且没有进入他的for循环,我的 function 没有返回任何东西。 Yours helps would be welcome.欢迎您的帮助。

Here is my code这是我的代码

Stream<List<InfoCompagnie>> fetchCompanies(
    String depart, String arrivee, String jour, int place) async* {
  var q = await db.collection('DISPONIBILITE').snapshots();
  await for (var q1 in q) {
    for (var compagnies in q1.documents) {
      var NomCompagnie = compagnies.data[firstCaseDate];
      for (var compagnie in NomCompagnie) {
        await for (var q2 in qq) {
          if (q2.documents.isNotEmpty) {
            //Making condition here
            for (var data in q2.documents) {
              await compagnieList
                  .add(infoCompagnie.value); //add Object here into list
            } 
            yield compagnieList; //return stream data here
          }//Function stopped here
        }
      }
    }
  }
}

You should only await futures, not streams.你应该只等待期货,而不是流。

Here await is not necessary in most of your code, since db.collection('DISPONIBILITE').snapshots() returns a stream. And the rest of your code seems synchronous.这里await在您的大多数代码中都不是必需的,因为db.collection('DISPONIBILITE').snapshots()返回 stream。您的代码的 rest 似乎是同步的。 Except for compagnieList.add(infoCompagnie.value) , which is a future and requires await .除了compagnieList.add(infoCompagnie.value) ,这是一个未来并且需要await

A Future represents a computation that doesn't complete immediately. Future 表示不会立即完成的计算。 Where a normal function returns the result, an asynchronous function returns a Future, which will eventually contain the result.正常的 function 返回结果,异步的 function 返回一个 Future,它最终将包含结果。 The future will tell you when the result is ready.结果准备好时,未来会告诉你。

A stream is a sequence of asynchronous events. stream 是一系列异步事件。 It is like an asynchronous Iterable—where, instead of getting the next event when you ask for it, "the stream tells you that there is an event when it is ready."它就像一个异步的 Iterable——在这种情况下, “stream 会告诉您准备就绪时有一个事件”,而不是在您请求时获取下一个事件。

That last line also explains why you shouldn't wait on streams: the stream tells you that there is an event when it is ready.最后一行还解释了为什么您不应该等待流: stream 告诉您准备就绪时有一个事件。

source资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM