Asynchronous 在Flatter/Dart中使用StreamBuilder仅从Firestore加载特定数据
我正在尝试使用StreamBuilder从Firestore加载数据。Asynchronous 在Flatter/Dart中使用StreamBuilder仅从Firestore加载特定数据,asynchronous,stream,dart,flutter,Asynchronous,Stream,Dart,Flutter,我正在尝试使用StreamBuilder从Firestore加载数据。 在我的名为“connect”的收藏中,有许多文档。 但我只想加载特定文档。 'connections'是一个列表,其中包含'connect'集合中的一些键。 'hello'是一个查询快照列表,其中包含'collection'集合中的一些文档 例如,如果my DB包含文档ID为以下内容之一的文档: -LK5SAToCPhI1Zp5W_bL -LK5Ypv0HeDCwcN4K41M -LK5j-OGtNjMpgklUB4B -L
在我的名为“connect”的收藏中,有许多文档。
但我只想加载特定文档。
'connections'
是一个列表,其中包含'connect'
集合中的一些键。'hello'
是一个查询快照列表,其中包含'collection'
集合中的一些文档
例如,如果my DB包含文档ID为以下内容之一的文档:-LK5SAToCPhI1Zp5W_bL
-LK5Ypv0HeDCwcN4K41M
-LK5j-OGtNjMpgklUB4B
-LK5mOih9wuz5ZSebXMn
“连接”列表仅包含以下部分:-LK5SAToCPhI1Zp5W_bL
-LK5Ypv0HeDCwcN4K41M
在StreamBuilder中,我只希望加载连接中具有相同名称的文档。如何仅加载特定文档
请帮帮我
Firestore.instance.collection('connect')
.snapshots()
.listen((docSnap) {
for (DocumentSnapshot docs in docSnap.documents) {
if (connections.isNotEmpty) {
for (String keys in connections) {
if (docs.documentID.toString() == keys) {
hello.add(docs);
}
}
}
}
});
@覆盖
小部件构建(构建上下文){
归还新脚手架(
正文:中(
子:列(
儿童:[
划船(
儿童:[
填充物(
填充:仅限边缘设置(底部:60.0),
),
扩大(
子:文本(
“邀请!”,
样式:TextStyle(fontWeight:fontWeight.bold),
textAlign:textAlign.center,
),
),
],
),
灵活的(
孩子:StreamBuilder(
流:Firestore.instance.collection('connect').snapshots(),
建设者:
(b)上下文,
异步快照(快照){
如果(!snapshot.hasData)返回常量文本(“加载…”);
final int messageCount=snapshot.data.documents.length;
返回ListView.builder(
填充:常量边集。对称(水平:8.0),
itemCount:messageCount,
itemBuilder:(\ux,int索引){
最终文档快照文档=
快照.数据.文档[索引];
如果(document.documentID==
您好[index].documentID){
返回容器(
孩子:排(
儿童:[
文本(“Hello$messageCount”)
],
),
);
}否则{
返回容器(
孩子:排(
儿童:[文本(“错误”)],
),
);
}
});
},
),
),
_buildLayout(),
],
)));
}
您找到解决方案了吗?
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 60.0),
),
Expanded(
child: Text(
"Invitation!",
style: TextStyle(fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
],
),
Flexible(
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('connect').snapshots(),
builder:
(BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text("Loading ... ");
final int messageCount = snapshot.data.documents.length;
return ListView.builder(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
itemCount: messageCount,
itemBuilder: (_, int index) {
final DocumentSnapshot document =
snapshot.data.documents[index];
if (document.documentID ==
hello[index].documentID) {
return Container(
child: Row(
children: <Widget>[
Text("Hello $messageCount")
],
),
);
} else {
return Container(
child: Row(
children: <Widget>[Text("Wrong")],
),
);
}
});
},
),
),
_buildLayout(),
],
)));
}