【发布时间】:2020-12-07 19:21:35
【问题描述】:
我正在使用 StreamBuilder 仅从文章集合中获取满足条件的文档。我使用了 where 子句,但它正在获取所有文档并对其进行排序。 (注意:我只想要那些满足条件的文档,而不是全部。) 更新的代码详细信息:使用另一个流构建器从用户集合文档中的以下字段获取 id。
我是怎么做到的:
StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('users')
.doc(profileScreenController.currentUser.uid)
.snapshots(),
builder: (context,
AsyncSnapshot<DocumentSnapshot> futureSnapShot) {
if (!futureSnapShot.hasData &&
futureSnapShot.data == null) {
return SizedBox();
}
DocumentSnapshot documentSnapshot =
futureSnapShot.data;
return Container(
height: screenHeight * 0.32,
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('articles')
.where('userId',
isEqualTo: documentSnapshot['following'])
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot> snapshot) {
print(documentSnapshot['following']);
if (!snapshot.hasData &&
snapshot.data == null) {
return Center(
child: Lottie.asset('assets/loading.json'),
);
}
if (snapshot.data.docs.length == 0) {
return Center(
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Lottie.asset('assets/no articles.json',
height: 140),
SizedBox(
height: 10,
),
Text(
'No body yet',
style: TextStyle(
fontSize: 20,
color: Theme.of(context)
.textTheme
.headline1
.color,
fontWeight: FontWeight.bold,
),
),
],
),
);
}
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.docs.length,
padding: EdgeInsets.only(left: 15),
itemBuilder: (context, index) {
selectedIndex = index;
DocumentSnapshot todaysArticles =
snapshot.data.docs[index];
return Container(
width: screenWidth * 0.8,
margin: EdgeInsets.only(right: 6),
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(30),
),
child: Text(todaysArticles.data()
['title'],
),
);
});
},
),
);
}),
【问题讨论】:
-
请编辑问题以更详细地解释什么不符合您的预期。由于我们看不到您传递给
where的变量的值,也看不到您数据库的内容,因此我们无法判断该查询应该返回什么。如果返回的文档与您的期望不符,您应该在此处显示。 -
@DougStevenson 已编辑问题.. 请立即查看 :)
-
我们仍然看不到您传递的变量的值。
profileScreenController.followerName
标签: flutter dart google-cloud-firestore