如何在Dart中的流构建器中切换正在收听的流? [英] How can i switch the stream I am listening to in a streambuilder in Dart?

查看:29
本文介绍了如何在Dart中的流构建器中切换正在收听的流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,当页面第一次加载时,我想使用1个按地理位置查询的流,但是要检查变量socialVisible为true的文档,然后当用户单击按钮进行切换时,我希望该流仍然通过以下方式查询地理位置,但现在使用profVisible字段检查文档.

Basically when the page first loads i want to use 1 stream that queries by geolocation but checks for documents where variable socialVisible is true, and then when the user clicks on a button to switch, i want the stream to instead still query by geolocation but now checks the documents with the field profVisible.

从理论上讲,这非常简单,每当按下相应的按钮时,我都会设置状态,以使socialPressed = true和profPressed = false,然后在streambuilder中,我使用三元运算符根据真实值来决定应使用哪个流of socialPressed和profPressed.

In theory this is very simple, whenever the corresponding button is pressed i set the state so that socialPressed=true and profPressed=false and then inside the streambuilder i used ternary operators to decide which stream should be used based on the truth values of socialPressed and profPressed.

但是,这仅在第一次单击时有效,socialPressed的初始值为true,因此初始流为社交流,然后当我单击以转到教授时,它将成功切换流.但是,在这段时间之后,当我尝试返回社交网站时,教授的所有文档都保留了,而不是被社交网站所取代?即使我将初始流切换为教授,也会发生这种情况,而且我不明白为什么会发生这种情况?

However, this only works on the first click, the initial value of socialPressed is true so the initial stream is the social one and then when i click to go to the prof one it switches streams successfully. However after this time when i try to go back to social all the documents from prof remain instead of being replaced by social? This happens even when i switched the initial stream to prof, and i do not understand why this is happening?

StreamBuilder(
            stream:socialPressed==true?socStream:proStream,
            builder:
                (context, AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
              if (!snapshots.hasData) {
                return Container(
                    alignment: FractionalOffset.center,
                    child: CircularProgressIndicator());
              } else {
               return Container(
                    child: Container(
                      child: (snapshots.data.length == 0)
                          Container(
                        height: MediaQuery.of(context).size.height * 2 / 3,
                        child: ListView.builder(
                          itemBuilder: (context, index) {
                            DocumentSnapshot doc = snapshots.data[index];
                          return UserTile('this is where i use the doc data')},
                          itemCount: snapshots.data.length,
                        ),),));}},)

这是我代码的基本结构,我删除了不相关的信息.

This is the basic structure of my code, i have removed irrelevant info.

请求了按钮回调代码:

 FloatingActionButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(16.0))),
                onPressed: () {
                  setState(() {
                    likeType='social';
                    socialPressed=!socialPressed;
                  });
                },
                elevation: 0,
                heroTag: 'socialButton',
                backgroundColor: socialPressed==false?Colors.grey[100]:Color(0xFFe0bdff),
                child: Icon(
                  Entypo.drink,
                  color: Color(0xFF8803fc),
                ),
              ),
              FloatingActionButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(16.0))),
                onPressed: () {
                  setState(() {
                    likeType='prof';
                    socialPressed=!socialPressed;
                  });
                },
                elevation: 0,
                heroTag: 'profButton',
                backgroundColor: socialPressed==true?Colors.grey[100]:Color(0xFFb9ebe9),
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: MediaQuery.of(context).size.width / 30,
                    ),
                    Icon(
                      FontAwesome.graduation_cap,
                      color: Color(0xFF096664),
                    ),
                  ],

                ),
              ),

推荐答案

我也遇到了这个问题.我正在使用RX Dart,并尝试根据布尔变量切换流.

I too faced this issue. I am using RX Dart and tried to switch streams based on a boolean variable.

我最初是在流中使用PublishSubject.切换到行为主体"已为我解决了该问题.目前,我尚不足以了解其工作原理,但会尝试对其进行进一步探索,并在可能的情况下在此处进行编辑.

I was initially using PublishSubject for my stream. Switching to Behaviour Subject solved the issue for me. At this moment I don't have sufficient knowledge on why this works but would try to explore it further and edit here if possible.

先前的代码:

final _activeOrdersFetcher = PublishSubject<OrderListModel>();
final _completedOrdersFetcher = PublishSubject<OrderListModel>();

StreamBuilder<OrderListModel>(
      stream: isActive
          ? _ordersBloc.getActiveOrders
          : _ordersBloc.getCompletedOrders

新代码更改:

final _activeOrdersFetcher = BehaviorSubject<OrderListModel>();
final _completedOrdersFetcher = BehaviorSubject<OrderListModel>();

这篇关于如何在Dart中的流构建器中切换正在收听的流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆