简体   繁体   English

我如何修改代码以获得 Stream<dynamic> 为 stream 生成器键入 stream?</dynamic>

[英]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.

相关问题 如何创建 Flutter stream 构建器? - How do I create Flutter stream builder? 如何使用 stream 构建器正确读取数据。 我不断收到空白页 - How do I read data with stream builder correctly. I keep getting blank page 如何使 stream 构建器购物车项目可通过触摸滚动? - How can I make stream builder cart items scrollable by touch? 制作流构建器 stream:一个变量会破坏 stream 构建器 - Making the streambuilder stream: a variable breaks stream builder 使用 Stream Builder 显示本地通知 - Show local notification with Stream Builder Stream builder 从 firestore 到 flutter - Stream builder from firestore to flutter 元素类型 'Stream<dynamic> ' 不能分配给列表类型 'Stream <query snapshot<object?> >'</query></dynamic> - The element type 'Stream<dynamic>' can't be assigned to the list type 'Stream<Query Snapshot<Object?>>' 如何组合或合并 firestore 的两个 stream? - How do i combine or merge two stream of firestore? 参数类型 'Stream<dynamic> ?' 不能分配给参数类型 'Stream<querysnapshot> ?'</querysnapshot></dynamic> - The argument type 'Stream<dynamic>?' can't be assigned to the parameter type 'Stream<QuerySnapshot>?' 如何在 flutter stream 生成器中使用不同的 firestore 集合创建 where 条件? - How to make where condition with different firestore collection in flutter stream builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM