gpt4 book ai didi

firebase - 如何在Dart的streambuilder中切换正在收听的流?

转载 作者:行者123 更新时间:2023-12-03 03:05:01 25 4
gpt4 key购买 nike

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

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

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

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,
),),));}},)

这是我代码的基本结构,我删除了无关的信息。

编辑:
请求了按钮回调代码:
 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),
),
],

),
),

最佳答案

您使用什么逻辑来设置状态?似乎您正在使用两个变量socialPressedprofPressed。如果您只有两个状态,我会考虑仅使用一个变量。

没有您的代码,我们只能推测出问题所在。但这应该在按钮的onPressed回调中起作用:

onPressed: () => setState(() { socialPressed = !socialPressed; })

关于firebase - 如何在Dart的streambuilder中切换正在收听的流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57941662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com