繁体   English   中英

Flutter stream 如何在stream中拨打function

[英]Flutter stream how to call function in stream

我有这样的 stream

        StreamBuilder<QuerySnapshot>(
          stream: FireStoreGlobalService().getRooms(globalProviderState.getID),
            builder: (BuildContext context,
                AsyncSnapshot<QuerySnapshot> snapshot) {
              if (!snapshot.hasData) {
                return new Text("Loading");
              }
              
              return Container();
            }),

Function 密码是

 Future<Stream> getRooms(id) async{
     return FirebaseFirestore.instance
                    .collection(ROOM_COLLECTION)
                    .where(id)
                    .snapshots();
  }

它在 stream 上显示错误

The argument type 'Future<Stream<dynamic>>' can't be assigned to the parameter type 'Stream<QuerySnapshot<Object?>>?'

如果我这样使用它

stream : FirebaseFirestore.instance.collection(ROOM_COLLECTION).where(id).snapshots()

然后它的工作 我想将它用作 function 我想我需要更改 function type Future 但不知道如何解决这个问题。

您可以直接创建一个 Stream 块,而不是像这样将其创建为 Future :

您不必使用 stream.periodic。 您可以访问flutter的官方页面以获取替代方案:)

https://api.flutter.dev/flutter/dart-async/Stream-class.html

 var _dataStream = Stream.periodic(const Duration(seconds: 1), (x) 
 {
   // Your Action Here
   return FirebaseFirestore.instance
                .collection(ROOM_COLLECTION)
                .where(id)
                .snapshots();
 }),

并在 StreamBuilder 中使用它

      StreamBuilder<QuerySnapshot>(
        stream: _dataStream,
        builder: (BuildContext context,
            AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          
          return Container();
      }),

暂无
暂无

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

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