[英]How do i modify the code to have a Stream<dynamic> type stream for stream builder?
Inside Stream builder, it is asking me to put Stream<builder>
otherwise throwing an error,在 Stream 构建器中,它要求我放置
Stream<builder>
否则会引发错误,
type 'Future<dynamic>' is not a subtype of type 'Stream<dynamic>'
Now since I was following this answer , i had to code the stream something like this,现在因为我一直在关注这个答案,所以我不得不像这样对 stream 进行编码,
getGroupsOfUser() async {
String _userID = await _getUID();
DocumentSnapshot userInfo = await userCollection.document(_userID).get();
return groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();
}
Now I know maybe if I could overcome using async I may fix it but I need to get the uid and the array of groups which is an async function and I can't really assign them as variables otherwise I'm getting the error only static members can be accessed in initializers
现在我知道也许如果我可以克服使用异步我可以修复它但我需要获取 uid 和组数组这是一个异步 function 我不能真正将它们分配为变量否则我
only static members can be accessed in initializers
Please, someone, help me I am very beginner in this.请有人帮助我,我是这方面的初学者。
The error must be from the line错误必须来自行
return groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();
Because.snapshots() method returns a Stream instead of a Future, and since your getGroupsOfUser() is a Future, it is throwing an error that you cannot return a Stream from a function that returns a Future.因为.snapshots() 方法返回 Stream 而不是 Future,并且由于您的 getGroupsOfUser() 是 Future,因此它会抛出一个错误,您无法从返回 Future 的 function 返回 Stream。
My solution: 1 - Put the logic for getting the userID and the userInfo inside the initState() ie make a Future for it.我的解决方案: 1 - 将获取 userID 和 userInfo 的逻辑放在 initState() 中,即为它创建一个 Future。 2 - Seperate the last line from the Future and wrap it with a StreamBuilder after successfully getting the userinfo from step 1.
2 - 将最后一行与 Future 分开,并在从步骤 1 成功获取用户信息后用 StreamBuilder 包装它。
getGroupsOfUser()
should return a Stream
since snapshots()
returns a Stream
. getGroupsOfUser()
应该返回Stream
,因为snapshots()
返回Stream
。 Therefore you need to do the following:因此,您需要执行以下操作:
Stream<QuerySnapshot> getGroupsOfUser() async* {
String _userID = await _getUID();
DocumentSnapshot userInfo = await userCollection.document(_userID).get();
yield* groupCollection.where(FieldPath.documentId, whereIn: userInfo.data['groups']).snapshots();
}
Since you need to return a stream then you need to use async*
and use the yield*
keyword to return.由于您需要返回 stream 那么您需要使用
async*
并使用yield*
关键字返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.