[英]How to merge multiple streams inside a stream
我正在尝试使用标签栏和标签栏视图来显示火基地的一些元素。 首先,我使用流构建器来获取选项卡栏中选项卡的文本:
class HomePage extends StatelessWidget {
final FirebaseUser user;
HomePage({this.user});
@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("places").snapshots(),
builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot){
if (!snapshot.hasData){
return Center(child: CircularProgressIndicator());
}
else{
return DefaultTabController(
length: 20,
child: Scaffold(
appBar: AppBar(
title: Text("Home Page"),
bottom: TabBar( isScrollable: true,
tabs: new List.generate(snapshot.data.documents.length, (index) {
return new Tab(child: Text(snapshot.data.documents[index]['name'].toString().toUpperCase()));
}),)),
然后我想从消防商店获得一个名为“temps”的集合的流构建器,其中包含文档,每个文档 ID 代表另一个名为“users”的集合中的文档 ID。 在用户的每个文档中,我都有一个名为 place 的字段。 我已经制作了选项卡并且它可以工作但是,我不能做的是:想要获取集合临时文件中每个文档的文档 ID,并获取此文档 ID 并使用它来访问在“用户”中具有相同 ID 的文档" 收集并检查字段位置是否与标签栏中的名称具有相同的值,我想将其显示在标签栏视图中! 我怎样才能做到这一点?
如果我理解正确,一种解决方案是在其State
内部创建一个StatefulWidget
,使用本地StreamController
并将您的StreamBuilder
指向它。
分别使用两个 Streams 并将这些项目添加到您的 StreamController。
它看起来有点像这样:
class YourClass extends StatefulWidget {
... createState() ...
}
class _YourClassState extends State<YourClass> {
StreamController<YourItem> _places;
@override
void initState() {
super.initState();
// the unified stream
_places = new StreamController();
// listening to changes of the first reference
CollectionReference places1Ref = Firestore.instance.collection("places1");
places1Ref.listen((snapshopt) {
// posting item to the unified streamController
_places.add(item);
});
// listening to changes of the second reference
CollectionReference places2Ref = Firestore.instance.collection("places2");
places2Ref.listen((snapshopt) {
// posting item to the unified streamController
_places.add(item);
});
}
@override
Widget build(BuildContext context) {
return StreamBuilder<YourItem>(
stream: _places.stream, // using here only the unified stream
builder: (context, snapshot) {
return YourWidgets();
}
);
}
}
这个模型使用YourItem
作为统一对象,但你可以使用其他东西,包括dynamic
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.