[英]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.
所以我决定从另一个角度来解决这个问题。
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.
因为它工作正常。
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。这导致了一个问题。
getPatientCollection
)?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.