简体   繁体   English

为什么在 Flutter wih Firestore 中使用 foreach 方法后得到 null?

[英]Why do i get null after using the foreach method in Flutter wih Firestore?

I was trying to get a user document and its sub-collection, simultaneously.我试图同时获取用户文档及其子集合。 But I found that it is not possible within a query.但是我发现在查询中是不可能的。 So I have decided to approach the problem from another perspective.所以我决定从另一个角度来解决这个问题。


What do we have?我们有什么?

I have a getRelative() getter and it returns Stream<Relative> .我有一个getRelative()吸气剂,它返回Stream<Relative>

  Stream<Relative> get getRelative async* {
    List<Patient> patientList;

    patientList = await getPatientCollection
        .forEach((element) => element.patients)
        .then((value) => value);

      yield* usersCollection
          .document(_userEmail)
          .snapshots()
          .map((snapshot) => _relativeDataFromSnapshot(snapshot, patientList));
    
  }

getPatientCollection() is another getter method and it returns Stream<Pat.netList> . getPatientCollection()是另一种 getter 方法,它返回Stream<Pat.netList> I am not gonna give detail of the getter.我不会详细说明吸气剂。 Because it works fine.因为它工作正常。


What is the problem?问题是什么?

The problem is when I get Stream<PatientList> , I am getting a Stream .问题是当我得到Stream<PatientList>时,我得到一个Stream So, just to convert it to Future , I have used forEach method.所以,为了将它转换为Future ,我使用了forEach方法。 And it also works fine.而且它也可以正常工作。 I can see element.patients is not empty.我可以看到element.patients不是空的。 I can see inside of it and its return type List<Patient> as it should be.我可以看到它的内部和它应该的返回类型List<Patient>

But when I was debugging it, after forEach() has done its job, I have realized that it return null. And patientList become null. That causes a problem.但是当我调试它时,在forEach()完成它的工作后,我意识到它返回 null。而patientList变成 null。这导致了一个问题。


What am I asking?我在问什么?

  1. What does it cause this problem?是什么导致了这个问题? And how can I solve it?我该如何解决?
  2. Is there any better way to get subcollection without using another Stream to get patient subcollection ( getPatientCollection )?有没有更好的方法来获取子集合而不使用另一个 Stream 来获取患者子集合( getPatientCollection )?

Stream.forEach is the Stream equivalent of List.forEach (which returns void ). Stream.forEach是 List.forEach 的Stream等价List.forEach (返回void )。 It's for doing some operation on each element, not for returning a new collection.它是为了对每个元素进行一些操作,而不是为了返回一个新的集合。 If you want to convert a Stream<T> to a List<T> , you can doawait stream.toList() .如果要将Stream<T>转换为List<T> ,可以执行await stream.toList()

暂无
暂无

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

相关问题 如何在 flutter 中获取 firestore 文档的 documentid? - How do I get documentid of a firestore document in flutter? Flutter - Firebase RTDB,在 null 上调用了 forEach 方法 - Flutter - Firebase RTDB, The method forEach was called on null 如何使用 flutter 从 Firestore 获取子集合? - How do I fetch sub collection from firestore using flutter? 我如何从 flutter firestore 中的 querysnapshot 获取单个字段值 - How do i get the single field value from querysnapshot in flutter firestore 为什么在 Firestore Flutter 中更新数据后 DateTime 会转换为 TimeStamp? - Why DateTime is converted to TimeStamp after updating data in Firestore Flutter? 为什么我在托管 Flutter 应用程序时出现灰屏? - Why do I get grey screen when hosting Flutter app? Stream 构建器在使用 Firestore 发布的 flutter 应用程序上返回 null - Stream builder returns null on released flutter app using firestore 如何使用 flutter 中的 JSONserializable 在 Cloud Firestore 数据库中设置嵌套字段 - How do I set up a nested field in a Cloud Firestore database using JSONserializable in flutter 如何使用 Flutter、Firebase - Firestore 数据库根据日期对数据进行分组和列出? - How do I Group and List data according to date using Flutter, Firebase - Firestore Database? 如何获取在 flutter 中具有特定字段值的 Firestore 文档的 DocumentID? - How do I get the DocumentID of a firestore document that has a particular field value in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM